![]() |
Problem with the use of templates - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Problem with the use of templates (/showthread.php?tid=35833) |
Problem with the use of templates - El Forum - 11-12-2010 [eluser]MarvinLeRouge[/eluser] Hi, I'm a newbie to CodeIgniter and MVC, and i have a problem with the way to use templates (the generic concept, not specially the CI one). I explain : Let's say i have : 3 pages : - home - products - about which all have : - a header - a content (depending on the page) - a footer Header, content, and footer containing dynamic data, there should be a call to a template controller from any page, which would call a header controller, a content controller (depending on the page you're on), and a footer controller. And each of those controller should : - process data - call a view But i'm a newbie to MVC and CI, and i can't find the correct way to do that. 1) Is it the correct way ? 2) Whatever the answer, how to achieve that ? Thanks Problem with the use of templates - El Forum - 11-12-2010 [eluser]jrtashjian[/eluser] Well, to keep it simple you'd need to create a controller with the 3 pages. Your controller could be called 'Pages' and you would have methods for each page (home, products, about). You would process everything for each page in the specified controller method and load the views at the end, while passing the data needed for each view. Quick example: /application/controllers/pages.php Code: class Pages extends Controller Hope this helps! Remember, there is no right or wrong way to do something. Do what works for you. Problem with the use of templates - El Forum - 11-12-2010 [eluser]MarvinLeRouge[/eluser] Well, What bothers me in such a way is : - that there is in each "page method" a call to a view for header and footer, which seems redundant - that we call directly a view named header, though there might be data treatment to do for the header. And a treatment should be done in a controller, not a view Problem with the use of templates - El Forum - 11-12-2010 [eluser]WanWizard[/eluser] Use a template engine. Setup the template and all fixed template sections in the controller constructor. And load the others in the individual controller methods. see Colin's user guide for an explanation. Problem with the use of templates - El Forum - 11-12-2010 [eluser]MarvinLeRouge[/eluser] Thanks, i'm looking into your answers. If anyone want to elaborate to help a poor newbie, feel free :-) Problem with the use of templates - El Forum - 11-12-2010 [eluser]techgnome[/eluser] Here's how I deal with it... my controller looks like this: Code: function index() Then my template.php file looks like this: Code: <?php if ($content_view == '') {$content_view = 'site/mainpage';} ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> hmmm... actually... I do see a problem... as my content_narrow and content_wide does the loading of the content... Hmmm... Must investigate this further and clean that up. Not sure I like how that works at the moment. Even still, that should give you at least one idea on how to implement something like that. -tg Problem with the use of templates - El Forum - 11-15-2010 [eluser]MarvinLeRouge[/eluser] Hi, So you would opt for an option - i load all the data in the same controller, whichever page i'm on - then i send the data block to the template - and the template checks the data content to see what it should display Wouldn't it be better to have one "wrapper" controller, which would call the headers and footers controllers, and a content controller depending on the page you're on ? Problem with the use of templates - El Forum - 11-20-2010 [eluser]Unknown[/eluser] thanks for answer.Good work Problem with the use of templates - El Forum - 11-21-2010 [eluser]eboxhead[/eluser] I actually do something similar to this but I have created a library function that handles all this. Code: function myView($viewToGoTo,$headerAndFooter="both", $data = NULL) Basically what myView does is ask for the view page that you want to go to (home, about, contact) and then if you want to include both, either, or none of the header and footer, and optionally a data array you want to pass. It then loads the view template.php that looks like this: Code: <?php While I know you typically try to keep a view as thin as posible, this gives me ability to optionally load a header and footer based on the criteria of a page (a full page or just a popup?) It saves me time and code vs displaying 3 views from a controller. |