Welcome Guest, Not a member yet? Register   Sign In
newbie MVC question - where should header footer code go?
#1

[eluser]Bill the Grue[/eluser]
ok.

I'm still learning, following the tutorials, etc... I want to write my code "correctly". Should my menu and footer code go in my controller or in my view? Right now it's in my controller, and it works fine, but I'm confused if I should be using controller or a view for this stuff (I think I should be using view).


Code:
function index()
    {
        $this->load->helper('url');
        $this->load->view('menu');
        $this->load->view('welcome_message');
        $this->load->view('footer');
        
    }
}


Thank you in advance!

Bill
#2

[eluser]jedd[/eluser]
Hi Bill,

You are in a small clearing. There is a pile of leaves here.

Your view code - that produces HTML - should be in the views subdirectory.

It's up to you whether you generate view-snippets (feeding them back into variables in your controller code) and then dumping them out later in a main view, or if you generate the common bits (header, footer, etc) in your MY_Controller controller .. but you should generate all actual HTML within a view file, and all logic that wraps around that in your controller(s). Beyond that, I may have misunderstood the scope of your question.

It is likely that you need to read up more on MY_Controller, but that's just a gut feel from the tone of our question more than anything else.
#3

[eluser]Aken[/eluser]
You've got the right idea, but you're wording it in a way that makes you seem confused.

A view is what is going to be displayed - it has all your HTML and variables that will be parsed from the controller to be displayed, etc. A view will only show up if called from the controller.

I do headers/footers in the same way you've done in your example. In my controller, I pull any info I need for my header (titles, meta, etc), then load them into my header view. Then I do all my content, then load the footer view.
#4

[eluser]John_Betong[/eluser]
 
CodeIgniter is flexible and can easily be abused to suit your personal coding style.
 
As a general rule of thumb I try to have absolutely no Html code in the controllers, models, libraries and helpers. All Html display code is in the view files.
 
I am led to believe the basic MVC approach is to separate, supply and manipulate raw source data. All requests from a user's view go to controllers. The controller responds to the request supplies relevant data to be shown in a view.
 
 
 
#5

[eluser]Bill the Grue[/eluser]
Thank you so much for your help. I'm really having a blast with CI, and look forward to introducing my project when it's done.

It's a slow learning curve (I have a one month old daughter, which automatically makes learning everything longer) - but very satisfying. I got dx auth to work, and todays mission is to do more stuff with forms and inputs.

Thank you for your help.

Warmly,

Bill
#6

[eluser]beckmann[/eluser]
I'm confused. The general consensus seems to be no HTML in files other than views. However, in the tutorial linked from CI's tutorial page:
Derek Allard's Tutorial using AJAX, Scriptaculous, and Models he generates HTML(a list for auto-complete) in his model. Just wondering if anyone can explain the discrepancy.
#7

[eluser]Gerben van Eck[/eluser]
I just checked the source (not watched the Video), and he indeed is returning HTML from a model (function_model) which is not like it should be in a MVC application.
I think the reason here may be that the actual output is so simple he decided to take a shortcut.
#8

[eluser]jedd[/eluser]
beckmann - if your name is Derek, you can do whatever the hell you like .. but for mortals like you and me, we should stick with The Plan, specifically HTML generation occurs in the views only.
#9

[eluser]no-cost lab[/eluser]
Hi All,
another Newbie Question - is it allowed to declare a function like this one:

Code:
function show_sign_in_box()
{
    document.getElementById('sign_in_box').style.display = 'block';    
}

inside the <body> element or should it go in the <head> element?

Thanks in advance!

No-Cost
#10

[eluser]intractve[/eluser]
Javascript can technically go anywhere,

(jQuery plug)
You should check out jQuery (jquery.com)
your code can be written as

Code:
function show_sign_in_box()
{
    $('#sign_in_box').show();    
}
And yes you do not even need to put in in a function to call it.

But this is not really the place for javascript/jquery questions, jquery has its own good community you can check out.




Theme © iAndrew 2016 - Forum software by © MyBB