« Do you ";"? Alright, it's compiler naming time... »

April 21, 2004

PHP Easy Templating System

Easy Template System is a PHP script that allows for the easy creation of web pages based on a user-defined template. Think of it as a "php-stylesheet." Everyone has heard "separate your content from presentation" before.. and this seems to be a pretty good way to go about it.

You define a template with placeholders for variables, set those variables in a php page (either hard-coded the content, or pull it from a database), and then run the page variables against the template. The template will then generate the HTML, replacing variables where necessary. This will separate the structure of the page from the content itself. Of course, you can then use CSS to generate the visuals and adjust the look of the page structure.. but by using a template system like this you can update an entire website by changing one file, instead of possibly thousands (similar to just including portions of HTML at various points in a page).

Some of the advanced examples are pretty killer. It looks like this is pretty flexible system. Has anyone been using this for any production websites, or have any experience with it? What do you think? Feel free to post your thoughts about ETS in the comments.

http://ets.sourceforge.net/.

Comments

  • And what about XSLT? :)

  • there are a lot of such template systems.
    Smarty (http://smarty.php.net) appears to be the first.
    and i agree that XSLT is more flexible solution, cause it is language-independent and if u need to move your project from PHP to say ASP.NET or Java u can't use that template system while XSLT parser is available for almost every programming language

  • Eureka! Every non-lame php programmer is using some template system since his born.

    There are tens of some libs. Get some and start your new life. You are now no more llama :))

    I can recommend you some very simple library, like xtpl. Forget 'ifs' and other hard things - you can always define block and add logic to php code.

    Template = presentation only
    PHP code = bussiness logic

    (XSLT is too complicated. Very hard to understand for normal coders. Trust me.)

  • alex: Smarty the first? LOL! Fasttemplate (port of Perl CGI::FastTemplate) or Template from PHPLib maybe, but Smarty definitely not!

    it's too lame anyhow.. Do not use Smarty or you are in big trouble

  • llama: can you explain why XSLT is too complicated (to an eventually unnormal coder)

  • so on one hand we have Smarty, and on the other llama. and we're trying to decide which is lamer.. hmm.. if it were me, I'd go with the non-substantiated-comment-posting one

  • claus wahlers: XSLT adds another presentation layer. You have to get some XML from PHP script and transform it with XSLT.

    See this sample:

    PHP code:

    echo "<source>";
    ....database stuff (get dog info)...
    for (..all rows..) {
    echo "<dog name=\"$row[name]\"><data color=\"$row[weight]\"/>
    </dog>";
    }
    echo "</source>";

    transform with XSLT:

    <xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
    <xsl:template match="dog">
    <p>
    <b>
    <xsl:text>Dog: </xsl:text>
    </b>
    <xsl:value-of select="@name"/>
    </p>
    <p>
    <b>
    <xsl:text>Color: </xsl:text>
    </b>
    <xsl:value-of select="data/@color"/>
    </p>
    </xsl:template>
    </xsl:stylesheet>
    </source>

    -------------OR----------------

    PHP code:

    ....database stuff (get dog info)...
    for (..all rows..) {
    $template->assign("name", $row[name]);
    $template->assign("weight", $row[weight]);
    $template->parseblock("dog", true);
    }

    with template:

    <!-- blockstart:dog -->
    <p><b>Dog: {NAME}</b></p>
    <p><b>Color: {COLOR}</b></p>
    <!-- blockend:dog -->

    --------------
    what do you think is better to understand (and maybe edit and change with html editor) for coder with html knowledge and no programming skills? XSLT is another "programming language", not only simple presentation layer. For big projects maybe, for simple web pages unusable. You can write your own simple template engine with no more than 200 lines of code and port it to any language you like. It is worth the effort.

  • claus, I was referring to llama's flamebait comment regarding Smarty.

    to answer your question..:

    first of all, you seem to know quite a bit about this issue, so I don't see why you're asking this.. it sort of seems that someone with your knowledge you should be able to answer their own questions, as it applies to their particular case and application.

    now if you're just asking for opinions.. I'm not a big fan of XML/XSLT, and not at all of HTML - I am known to say that "I hate HTML, and I don't hate many things." I've worked on many projects (30+) of low-to-moderate complexity where I did the programming and multiple designers worked on the "presentation layer." I did research on the various PHP template engines and chose Smarty. neither I nor any of the designers have ever felt we made the wrong choice. I've actually been complemented for the "good call" to pick Smarty and run with it.

    as always, YMMV. but for me and 3 other people I work on these projects with, Smarty is absolutely perfect for our needs. yeah, it has small quirks, but those are easy to get around. and yeah, I could write my own engine, but it becomes a matter of where do I stop - I can write my own PHP in C, clone Apache, I can run it on my own distro of Linux (I've actually built one at one point), etc. for our moderate complexity projects writing our own template engine would consume a lot of time, time that I feel could be spent elsewhere rather than reinventing the wheel.

    BTW, in your above XSLT example, the XSLT has absolutely nothing to do with coding in PHP for templates. the PHP code looks exactly how it would look if you did not use XSLT - mixing the presentation layer (HTML, or HTML-like tags) with the "business logic." read: horrible

    p.s. the correct Smarty code for the dog's name in your example above is
    Dog: {$name}
    p.p.s. a better way to do it is
    // PHP code
    for (..all rows..)
      $temparr[] = array(
        'name' => $row['name'],
        ...
        );
    $template->assign('objects', $temparr);

    // Smarty tpl
    {section name=think_reusable loop=$objects}
    Dog: {$objects[think_reusable].name}<br />
    {/section}

  • If you are having trouble with Smarty, I would really recommend the book "Smarty for Dummies".

  • The problems with a template system are that they all have their own way of doing things. XSL is a standard. With PHP 5 there will be a new DOM model and XSL parser build in. I've seen it in action and it's really cool.

    I agree that XSL is looking far more complicated then a HTML template like posted above, but in practice it isn't much harder then working with tables in HTML.

  • Exactly Geeza. XML/XSLT are templates maintained by W3C...which alone makes them far more attractive.

    What happens if you wanted to pop out your PHP and replace it with Java? Can you do that easily with Smarty? I don't believe so...but with XML/XSLT you certainly can because it's a real template engine. If you're using a PHP-based template system, then you haven't achieved true seperation of layers one and two.

    I can change from PHP to Java or .NET, and not even tell my Web Designer! Try accomplishing that with Smarty. XML/XSLT is more complicated, just like Java is more complicated than PHP. But it's a small price to pay for massive reusability/extensibility IMHO.