Welcome Guest, Not a member yet? Register   Sign In
Template fragment solution, how to include other templates in a template.
#1

[eluser]Unknown[/eluser]
Hi,

I wanted to post a very simple and effective way to achieve merge fragmented sections in a template, i.e including other sections in your base template.

With this way, you can reuse sections of code, I used .tpl file extensions which is same format as smarty.

Code Igniter does a great job of this, I just think it was unclear how this was done.

Here is the Controller:

Code:
class Template extends Controller{

function index()
{
    // content.tpl holds header,body,footer
    $header = $this->load->view('header.tpl', '', true);
    $body = $this->load->view('body.tpl', '', true);
    $footer = $this->load->view('footer.tpl', '', true);
    $content = array(
        'header' => $header,
        'body' => $body,
        'footer' => $footer
    );
    
    // contents now holds all parsed html in $contents
    $contents = $this->load->view('contents.tpl', $content, true);

    $data = array(
        'contents' => $contents
    );
    // output all contents
    $this->load->view('index',$data);
}

}

Here is index.php (View)

Index.php

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;&lt;/title>
    &lt;link rel="stylesheet" type="text/css" href="style.css"&gt;
&lt;/head&gt;

&lt;body&gt;
    &lt;?php echo $contents;?&gt;
&lt;/body&gt;
&lt;/html&gt;

The rest of the files are now .tpl files which are included in the 'views' Directory, this of course is just your html broken up.

contents.tpl however includes - header.tpl, body.tpl and footer.tpl

contents.tpl looks like this:

Code:
<div id="doc4" class="yui-t2">                    
    &lt;?php echo $header;?&gt;
    &lt;?php echo $body;?&gt;
    &lt;?php echo $footer;?&gt;
</div>

Thats it, of course you can include templates and parse how you like depending on how you break up your templates. I just do

it in this way to match the template method i use.

One more thing,if you want to completely keep your PHP out of templates, you can use tags like {header} with code igniter, really you don't need smarty at all, however it is slower using tags than including the php, but i know this is sometimes needed.

I hope this helps someone out, as I've seen a few posts about this and i think this is probably the most effective way, and i don't think it breaks MVC.

many thanks
Ian
http://www.twitter.com/iancal
#2

[eluser]xwero[/eluser]
You can half the lines you wrote by removing the variables you create. The variables have no further use.
Code:
function index()
{
    // content.tpl holds header,body,footer
    $content = array(
        'header' => $this->load->view('header.tpl', '', true),
        'body' => $this->load->view('body.tpl', '', true),
        'footer' => $this->load->view('footer.tpl', '', true)
    );
    
    // contents now holds all parsed html in $contents
    $data = array(
        'contents' => $this->load->view('contents.tpl', $content, true)
    );
    // output all contents
    $this->load->view('index',$data);
}
You could see nested views as templates but mentioning smarty and using the .tpl extension puts another spin on your post. Either this is a tutorial how to nest views or this is a tutorial about nesting templates using a template engine. Mentioning a template engine is distracting which could throw readers off.
#3

[eluser]Phil Sturgeon[/eluser]
To make this even easier put that code into a plugin or library and use it to call different pages.

Code:
$this->layout->load('index', $data);

Much cleaner code for your controllers.
#4

[eluser]Unknown[/eluser]
xwero and Phil,

thanks very much for the feedback and code improvements.




Theme © iAndrew 2016 - Forum software by © MyBB