Template fragment solution, how to include other templates in a template. |
[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{ 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"> 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"> 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
[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()
[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.
[eluser]Unknown[/eluser]
xwero and Phil, thanks very much for the feedback and code improvements. |
Welcome Guest, Not a member yet? Register Sign In |