views:

122

answers:

2

What is the best, easiest, and fastest way to use a template for a page. Right now I have been using a function to create my template for each page.

$title = "Kick Me";
<?php pageHtmlHeadStart($title)>
    <!-- Were Javascript would go -->
<?php pageHtmlHeadEnd(); guiHeader(); ?>

    Content went here.

<?php guiFooter; ?>

I uses to use includes...

<?php include('header.php'); ?>

    Content went here.

<?php include('footer.php') ?>

I was thinking about using PHP Object to do the same thing too. It would run like the funtions way of doing it. I would create a GUI class tha would have the template wrapper and include some scripts to display content (via echo) that I use a few time over again on different page.

I probably will answer my own questions when say this... I like the current way do it with the functions. I just dont know if this is bad coding habbits.

A: 

If you want a template, then use a templating language. Using PHP for templating is definitely not a good practice according to me. Some good PHP templating engines are mentioned at http://digg.com/programming/Top_25_PHP_template_engines

pinaki
Even http://www.webresourcesdepot.com/19-promising-php-template-engines/ is a good link
pinaki
Php _is_ a templating engine. It's designed to make it easy to embed code in HTML. Using yet another layer of indireciton just makes everything more complicated.
gnud
I would like this to suggest this in the place I work, but we do currently have quite a few pros from templating with functions. Why is a templating language better? Are there any templating languages that allow for mini-template functions to print out often-used UI elements like skinned buttons, tabbed bars, etc... since this is what all of my colleagues have been using it for and I'd have trouble convincing them to use something new if they wouldn't be able to save time in this way...
lhnz
Multiple answers possible. Everybody likes it different. My taste: No template engine, just as gnud said, PHP "is a template engine".If you like to seperate your code from your html, you could use a template engine OR just create a codebase where you separate it yourself. My suggestion, take a look at the MVC pattern.
Frenck
You can think of PHP as a templating engine but with the increased complexity of web-page design it is always better to have the display layer move to another templating engine. I dont see how this would make things complicated. Rather I would say it makes things simpler. As for e.g., I can look for UI issues under templates (say smarty) thus separating the UI logic from the PHP functional logic. Any views??
pinaki
@pinaki - Thank you but a template/php framwork isnt what I am looking for right now.
Brian Ojeda
@lhnz - the whole purpose of templating engines is re-use you can create small template files in smarty (for different UI elements) and include them wherever you want. Look at the fetch() API of smarty as an example. Also, you can keep the rendering logic separate from the data logic.
pinaki
+2  A: 

In the context of templates, there is nothing wrong to use include() statements. They make clean looking code and makes it easy to share html snippets between templates.

Whether to use instance methods, static methods or functions for "template tags" depends on the architecture of your framework or application. While generally OOP is really good thing, making everything an object doesn't magically make your code better.

PHP itself does make a good template language. There is no need to invent another "language" for templates – neither is it wrong, as long as you cache your parsed files (like Smarty does).

You may want to check out Savant and Smarty template engines. I also recommend you to study various PHP MVC frameworks and see how things are implemented there: Symfony, Kohana.

jholster
Just beat me to say the exact same thing. :)
Joseph Mastey
Thank you Yaggo for the short answer. BTW, today Net Tuts+ released a short tutorial about MVC. I thought it goes along with your answer. http://net.tutsplus.com/tutorials/other/mvc-for-noobs/
Brian Ojeda