Loading views in templates - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Loading views in templates (/showthread.php?tid=26244) |
Loading views in templates - El Forum - 01-09-2010 [eluser]Daniel_E9[/eluser] I have a php view called 'blog' which has header, footer sections. I am currently loading these in like this: Code: <?php $this->load->view('header'); ?> I am currently parsing in my blog articles like this in the controller: Code: $this->parser->parse('blog', array('articles' => $articles)); I can't seem to figure out how to load these components/html into their respective {header} / {footer} areas in the view. Code: {header} Any ideas? Thanks Loading views in templates - El Forum - 01-09-2010 [eluser]Udi[/eluser] I don't recommend using this method with {tag} and parsers. Just print the variable itself, $header . I do recommend to write a template library that creates your template from these different views - header, footer, side bar and etc. The only thing that changes is the epicenter - the forms/tables in the middle, so the template library gets this epicenter content from your controller and prints it. Loading views in templates - El Forum - 01-09-2010 [eluser]Daniel_E9[/eluser] But how would I write a template library? I was trying to figure out how to have one controller that loads everything in (header, footer) etc - but I can't get my head around it. As for parsed tags - even if they are not recommended I'd still like to know how I could get the above to work. Thanks Loading views in templates - El Forum - 01-09-2010 [eluser]richfearless[/eluser] Hi Daniel_E9, I would suggest going and having a read on this page: http://codeigniter.com/wiki/MY_Controller/ Loading views in templates - El Forum - 01-09-2010 [eluser]Udi[/eluser] [quote author="Daniel_E9" date="1263076011"]But how would I write a template library? I was trying to figure out how to have one controller that loads everything in (header, footer) etc - but I can't get my head around it. As for parsed tags - even if they are not recommended I'd still like to know how I could get the above to work. Thanks[/quote] 1. Write new class called TPL, store in the application/libraries folder. 2. Create a method called 'do', this method gets $content. 3. All this method does, is: Code: function do($title, $content) { and now views/basicTemplate: Code: <?php echo $header; ?> so you create 3 views, basicTemplate, header, footer... and thats it. Loading views in templates - El Forum - 01-09-2010 [eluser]Daniel_E9[/eluser] I have semi figured this out, but wondering if there is a slightly cleaner way? At the top of my blog controller I now have: Code: $header = $this->load->view('header', '', true); And in the parser I now have this: Code: $this->parser->parse('blog', array('articles' => $articles, 'header' => $header, 'footer' => $footer)); Is there a cleaner way of putting all this data into the parser rather than for each header/footer/sub template areas - Like below etc? Code: $this->parser->parse('blog', array('articles' => $articles, 'template' => $template)); Thanks for the replies so far - I will def look into them too. Loading views in templates - El Forum - 01-09-2010 [eluser]Udi[/eluser] Yes, Create a library class that doing all this for you, and you send to in minimal data each call. |