Gene_Template

With Geneone I decided to get rid of Smarty for several reasons:

  • I wanted to internationalize my script. Smarty could do it, but I’d end up with code such as {"Entry Title:"|tr}. This is fine but it quickly gets ugly.
  • I wanted to filter the output for HTML. At the moment I have been doing it in a hackish way by calling htmlentities() before it was passed to Smarty. This was pretty ugly and involved more work than neccessary.
  • I’m doing a lot of advanced things which outgrow the syntax of Smarty. I’ve had to use {$object|@makelink} when $object is an array/object and {$object|makelink} when $object is a string.
  • I was getting to the point where I’d have to create function in PHP then interface it with Smarty. It was easier to go skip that extra step. In PHP you can do all the familiar stuff such as substr($str, 0, 3) instead of {$str|substr:0:3}.
  • A lot of people don’t seem to like Smarty. Templates in PHP are good because it’s already familiar to all PHP programmers. Template designers are more likely to know PHP than Smarty. If they don’t, they may learn a bit of PHP and the fact they know a little bit might mean they would be more likely to contribute to the core code.
  • Smarty is LGPL. Eventually I want Geneone to become a fully BSD licensed package.

Smarty had actually suited me well for the last couple of years so it was sad to see it go. I looked at Savant which is probably the most well known templating system which uses PHP but I decided that it was too large and the LGPL license wasn’t BSD-compatible.

Savant has template plugins which I didn’t really see the point on. Instead, I simply define a function either at the top of the template or in the functions.php file. Instead of ~300KB for Smarty and ~40KB for Savant, Gene_Template is just over 3KB. The API has been modelled after Smarty so a lot of the code from Smarty doesn’t have to change.

The API looks a bit like this:

class Gene_Template {
    var $templateDir;
    var $templateVars = array();
   
    /**
     * Assigns a value to a template variable.
     *
     * @var string $name Variable Name
     * @var string $value Variable Value
     */
    function assign($name, $value) {}
   
    /**
     * Assigns a value to a template variable by reference.
     *
     * @var string $name Variable Name
     * @var string $value Variable Value
     */
    function assignRef($name, &$value) {}
   
    /**
     * Appends a value to a template array variable.
     *
     * @var string $name Variable Name
     * @var string $value Variable Value
     */
    function append($name, $value) {}
   
    /**
     * Appends a value to a template array variable by reference.
     *
     * @var string $name Variable Name
     * @var string $value Variable Value
     */
    function appendRef($name, $value) {}
   
    /**
     * Gets the value of a template variable.
     *
     * @var string $name Variable Name
     *
     * @return mixed Variable Value
     */
    function get($name) {}
   
    /**
     * Get an array of assigned template variables.
     *
     * @return array Assigned Variables
     */
    function getAssigned() {}
   
    /**
     * Fetches a template and returns the contents.
     *
     * @var string $templateName Template Name
     * @var string $variables Template Variables
     *
     * @return string Parsed Template Content
     */
    function fetch($templateName, $variables=array()) {}
   
    /**
     * Fetches a template and displays the contents.
     *
     * @var string $templateName Template Name
     * @var string $variables Template Variables
     */
    function display($templateName) {}
}

Templates change from Smarty syntax a bit like this:

{if $actions}<ul id="actionlinks">
{foreach from=$actions item=i}
    {if $i.2} {* Current Action *}
        <li class="currentact">{$i.1}</li>
    {else}
        <li><a href="{$curlbase}/{$i.0}">{$i.1}</a></li>
    {/if}
{/foreach}
</ul>{/if}

To PHP Syntax a bit like this:

<?php if ($actions): ?>
    <ul id="actionlinks">
    <?php foreach ($actions as $i): ?>
        <?php if ($i[2]): /* Current Action */?>
            <li class="currentact"><?php _e($i[1]) ?></li>
        <?php else: ?>
            <li><a href="<?php echo $curlbase ?>/<?php echo $i[0] ?>"><?php _e($i[1]) ?></a></li>
        <?php endif; ?>
    <?php endforeach; ?>
    </ul>
<?php endif; ?>

The Smarty syntax probably looks a bit cleaner and it’s a lot lazier to type but I hope things such as syntax highlighting and familiarity with PHP will counteract the benefits of Smarty. 

Leave a Reply

Your email address will not be published. Required fields are marked *