Welcome Guest, Not a member yet? Register   Sign In
Best way to avoid duplicating html in views
#8

[eluser]Polarity[/eluser]
I had the same problem. what i did it was the following.

i created one view that i called layout. that view contains the complete site layout. header, content, sidebar footer.
every section have a variable like that:
Code:
<div id="ss_sidebar">
    &lt;?=$sidebar?&gt;
</div>

<div id="ss_content">
    <h1>&lt;?=$site_title?&gt;</h1>
    &lt;?=$content?&gt;
</div>

then i created a helper function. that function do nothing more, than render this layout view and fill it with code at the regions. put this helper in the autoload to have it global.

Code:
function layout($array)
    $layout['site_title'] = (!isset($array['site_title']))?'standard text':$array['site_title'];
    $layout['site_name'] = (!isset($array['site_name']))?'standard text':$array['site_name'];
    $layout['content'] = $array['content'];
    $layout['sidebar'] = $array['sidebar'];
    $CI->load->view('layout',$layout);
}

so in the controllers i have nothing more to create an array an pass it to my helper function

Code:
function blog {
    $data = array(
        'content'=>$this->load->view('forum/list',$data,true),
        'sidebar'=>$this->load->view('forum/newArticles',$data,true),
        'site_title'=>'My Website'
    );
    layout($data);
}

the good thing on this method is, that you dont have view calls inside the view and you can use views for ajax calls (updating only the content, would be a problem because you call a view inside the view.). And you can always add widgets or new regions to your layout without changing many views or run into a including chaos. i think its always a good practice too keep code duplication minimal.

Edit: if you ever want to put ajax (jquery etc) on your site and you want to refresh the content only, you can just do something like
Code:
function blog {
    $data = array(
        'content'=>$this->load->view('forum/list',$data,true),
        'sidebar'=>$this->load->view('forum/newArticles',$data,true),
        'site_title'=>'My Website'
    );

    // render the site
    if($ajax) // ajax call?
    {
        // yes, only the content please
        echo $data['content'];
    else
    {
        // rende the complete site
        layout($data);
    }
}


Messages In This Thread
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 08:50 AM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 09:31 AM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 10:24 AM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 10:35 AM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 10:46 AM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 01:10 PM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 01:27 PM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 05:44 PM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 06:24 PM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 06:24 PM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 06:54 PM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 08:04 PM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 08:13 PM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 08:18 PM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 08:19 PM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 08:26 PM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 08:27 PM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 08:29 PM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 08:30 PM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 08:32 PM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 08:42 PM
Best way to avoid duplicating html in views - by El Forum - 12-18-2008, 09:04 PM



Theme © iAndrew 2016 - Forum software by © MyBB