Lightweight PHP5 based template class/system

TWIG

I would recommend using Twig

  • extensible syntax
  • efficient
  • compiles and caches your templates to PHP classes with a very small overhead
  • sandbox mode to evaluate untrusted template code
  • unit tested
  • great documentation
  • multiple template inheritance, template blocks, automatic output-escaping

Also read Fabien Potencier's blog post, where he explains needs for a powerful and customizable template engine.

TWIG Template code

{% extends "layout.html" %}

{% block title %}
    {{ page.title|escape|title }}
{% endblock %}

{% block content %}
    Content of the page...

    {% for user in users %}
      * {{ user.name }}
    {% else %}
        No user has been found.
    {% endfor %}

{% endblock %}

{# this is a comment in twig syntax #}

Symfony Components

Also if you need additional components for web development, but you already have a defined code base, have look at Symfony Components which includes additional templating component (mentioned in XUE Can answer)


I wrote Nest. It's a templating language based on the better parts of JSP, and it compiles to php code. You write your code in well-formed HTML and can easily create new tags to give you new functionality. There are some standard tags built in.

http://nest.sourceforge.net/

Looks like this:

<html xmlns:c="urn:nsttl:HTML_Template_Nest_Taglib_Standard">
  <body>
    <ul>
      <c:foreach items="posts" var="post">
        <li>${post->title}</li>
      </c:foreach>
    </ul>
</body>

PHP by itself is already a template engine. So why not cut out the overhead a template engine written in a template engine brings with it and just use PHP then?

<h1><?php echo $pageTitle ?></h1>
<div>
    <ul>
    <?php foreach($items as $item): ?>
        <li><?php echo htmlentities($item); ?></li>
    <?php endforeach; ?>
    </ul>
</div>

If you need added functionality, consider using ViewHelper, e.g. small functions that encapsulate stuff like adding links names or translating, e.g.

<table>
<?php foreach($items as $key => $item): ?>
    <tr class="<?php echo oddEven($key)?>">
        <td><?php echo productLink($item->id); ?></td>
        <td><?php echo translate($item->description); ?></td>
    </tr>
<?php endforeach; ?>
</table>

If that's too verbose, have a look at HEREDOC and NOWDOC syntax and if this is still not what you are looking for, here is a list of some template engines:

  • http://www.webresourcesdepot.com/19-promising-php-template-engines/
  • http://en.wikipedia.org/wiki/Web_template_system#Server-side_systems

Or, if you feel experimental, have a look at XHP, Facebook's extension approach to a Template engine:

  • http://www.facebook.com/notes/facebook-engineering/xhp-a-new-way-to-write-php/294003943919
  • http://toys.lerdorf.com/archives/54-A-quick-look-at-XHP.html
  • https://github.com/hhvm/xhp-lib

I just released an open source project which makes this really easy. It is "Template Inheritance", inspired by Django's, and will let you inherit and override parts of a parent template from a child template. Located here:

http://arshaw.com/phpti/

Tags:

Php

Templates