Best templating method? |
[eluser]KennyBrijs[/eluser]
Hi all A common annoyance is having to call things like a header/footer etc time and again. I've heard there is a templating engine, but don't know if this is the best solution for me. I've also been searching but haven't found something obvious and easy. Which kind of surprises me since this is a very common task. So, I need the typical header (with a little login/register part), footer and sidebar with content (latest posts). What is the best way to have this automatically for all pages, also being able to have exceptions? E.g.: different content, no login/register, ... (these may be just different templates). Greetings Kenny
[eluser]coffeeandcode[/eluser]
I use a template library with a render() method. The render method takes in an array of content views to load, and any needed data. It then loads the content views into a variable, then loads the template view (I have one template view, instead of header, footer, etc) which echos the $content variable in the right place. Since the content is handled by the content views, the only exceptions to handle would be changes in the template. I would handle that with variables passed to the render function. The template can check if a particular element should be rendered or not: Code: <?php if ($sidebar) : ?>
[eluser]Jelmer[/eluser]
I've treid a couple of ways, the 2 I liked most are: - Writing my own "view library" that I call once at the end of every controller and which takes care of all common tasks. This is most suited if you need a lot of different kinds of output and complex methods to compute them. - Use a base controller like this one from Jamie Rumbelow. I'm not using it directly but it was the inspiration for templating in some of my apps. I like this way a lot when the controller almost always needs default templating behavior and has only few deviations. Edit: Also I prefer to use a wrapper that loads other views within it depeding on a certain variable that's past to it. I prefer that over using a header and a footer, but I guess that's more a personal preference than a best practice. |
Welcome Guest, Not a member yet? Register Sign In |