Savant

I finally decided to investigate Savant today, a "simple, elegant and powerful" alternative to Smarty.

Smarty is undoubtedly the king of PHP templating engines. It’s powerful, very flexible and can be quite beautiful. I’ve used some quite advanced features of Smarty. In Evolution we use Smarty to do callbacks from the forum code to Evolution code.

One of the features I’ve liked about Smarty is the ease and elegance of embedding parts of different templates. For example $object contains an array which is the current forum discussion. $childs contains all the posts. In Smarty you can do the following:

{$object.title}
{foreach from=$childs item=i}
{include file="showpost.tpl" object=$i}
{/foreach}

In showpost.tpl, you can then refer to $object. Sure you can do it in loads of other stuff, but I really like the elegant syntax you can use in Smarty. On the other hand, Smarty can force you to do ugly stuff like this:

{tagcloudsize tags=$terms assign="total"}

{foreach from=$terms item=i}
    {tagsize tag=$i total=$total assign="size"}
    {"<em>"|str_repeat:$size}

This is one example of a limitation and fugly code in Smarty. You need to implement a callback function which takes an "assign" argument and then use what is returned.

Harry Fuecks walks through some of the issues with Smarty. 

PHP is a bit more complex than basic Smarty but once you get into the more complicated stuff, PHP turns out to be a lot simpler. A lot more people can code PHP too. 

I’m actually not sure whether I should use Savant or just roll my own templating code as Savant doesn’t actually seem to do much and I’d like to get away from using LGPL/GPL software. Anyone had experience with alternative templating engines?

2 thoughts on “Savant

  1. Why do you want to use a template engine?

    SMARTY is in my point of view one of the worst thing that ever hit PHP.

    The only valid argument I know about using a template engine other than php, which after all is a template language. Is if you want to make sure designers don’t have the luxury of messing up the site by "accidentally" write something like var_dump($_SESSION ) in one of your templates.

    As long as you separate your business and presentation layers, the only logic a template or view should include is foreach, for or while. Everything else is business logic and have nothing to do in a template.

    At least this is what I belive and pratice……

  2. At the moment I’m using Smarty simply because that was the templating engine I used for projects beforehand and have used since version 0.1. I’m looking to get rid of Smarty.

    Savant is a template system which uses PHP. I don’t think it does much apart from manage things such as assigned variables. Having things such as assigned variables makes sense. I don’t want a few billion variables which only apply to the template hanging around the main code and polluting the global namespace. 

Leave a Reply

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