Savant Template Engine

When we (as in my business partner and I) started developing the third version of our open source program (our first ever ‘commercial’ product), Olate Download, we had to decide how we were going to handle templates to allow the user to customise their site. At the time, the options available to us were to use Smarty (or find a different engine) or create our own template engine to handle it. We had a look at Smarty and decided it was too complex for our simple download management application. As such, we went ahead and created our own system.

Looking back at our engine now, it does its job, but it is limited. We added our own very basic control structure syntax and built in includes and even a simple language system. It did the job well.

Several months after we released that product, I started development on our latest product – a paid application to manage software sales. This too was to have a template system because it is easiest way to allow customisation of the product. I had recently read an article in PHP Magazine about an engine called Savant2. After playing around, I decided that this time, I didn’t want to create my own system, but I would use Savant to power the template system.

Side note: One of good things about using a 3rd party system rather than writing your own is that the developers of the product are usually dedicated to improving that product. They are not involved in your development so they work on their program, improving, bug fixing and updating. This means that part of your product improves without you needing to do much, except update your integration of it! You can work on other parts of your product whilst that bit is handled by someone else.

Comparing Savant to Smarty is difficult. Yes they are both template engines, but Savant does not compile its templates. Nor does it have its own built in scripting syntax. It uses PHP. Many people also consider Smarty to be ‘heavy’ with lots of complex features that aren’t necessarily needed in all situations. For example I didn’t need caching or compiling in my products and I also wanted to work with PHP syntax in the templates, not the custom syntax that Smarty has.

[quote=”Savant Website”]In short, PHP is itself a template language, so in general there is no need for another template language on top of it. However, there are some specific cases where using customized markup is safer than PHP; for that reason, Savant allows you to hook in a custom compiler for your own purposes.[/quote]

(Also see articles by Harry Fuecks and Brian Lozier)


The idea behind Savant is (what any PHP developer should be trying to do) to separate design from the application itself. i.e. separate the HTML from the PHP code that performs the majority of the functions. This is Smarty does 100% – you see no PHP in the templates. But in Savant, this is not the case. Whilst all the main code is in separate PHP files, there is some PHP in templates.

Savant always has 2 files. One of them is the PHP file which does most of the work and then other is a template file (usually with the .tpl.php extension).

To illustrate this, here is an example, where I have Savant2.php in the same directory:

$template = new Savant2(); // New Savant2 object

$title = 'Customers';
$list = array(array('name' => 'James', 'Country' => 'UK'), array('name' => 'Jill', 'Country' => 'Canada'));

$template->assign('title', $title); // Assigning a var to be used in the template
$template->assign('customers', $list);

$template->display('customers.tpl.php'); // Display the template now

In the HTML template customers.tpl.php, I would have this:

... HTML ...

customers)): ?>

title; ?>

customers as $key => $value): ?>

Customer Country

There are no customers.

... HTML ...

This would echo the 2 customers I added to the $list array. Notice how the variables assigned from the $template->assign() call are class variables using $this->varname. Also notice that I’m using PHP control structures (the shortened versions) within the template.


That is a very simple example and the real usefulness of Savant comes with its various plugins and filters.

[quote=”Savant Website”]Template plugins are objects you can call from your template to automate repetitive tasks. Savant loads plugin objects dynamically as you call them, so you don’t have to pre-load them. However, if you want to pre-load a plugin, you can sometimes configure its behavior in advance; whether or not a plugin can be configured depends on the specific plugin.[/quote]

The plugins available include form generation, image/CSS/JS display, and general options like date formatting. Of these, one of the most useful is the option list plugin.

One of the problems with dynamically generated option lists is selecting the default value. This is made easy with the options plugin.

For example, when editing a database record, I could have this code:

// Status list menu options
$status_options = array(1 => 'Active', 0 => 'Disabled');
$template->assign('status_options', $status_options);

This sets the status drop menu options and assigns them to the template variable. Elsewhere in the code, I have already obtained the data from the record in the database.

In the template, I can then create the list, populate it with the 2 options and also set a default value based on the data returned from my database record:

plugin('options', $this->status_options, $this->user['status']); ?>

Here, the plugin method is called. The first parameter is the name of the plugin, the second is the array of options and the third is the default value. This value could be at any point in the list, it only needs to reference the array key of the options you provide. The output might look like:


Another really useful plugin is the HTML checkbox. I’m sure everyone has encountered the problem of trying to determine the value of a ticked checkbox! Savant makes it easy with the checkbox plugin so it is easy to set a value when ticked. You can also just as easily have it set as a value when you’re editing a database record (for example) like with the option menu above.

Savant2 works with both PHP4 and PHP5 (Savant3 is written specifically for PHP5).

From working with Savant since around October last year, I have been able to do everything I want to do with it. I found that it has easily coped with a large application such as my own, and I expect it could easily scale both ways. It is not bloated, it is well coded (includes PHPDoc source documentation) and it does exactly what I need it to do.

There are plenty of engines around. It is all about finding one you like and that suits your project. You’ll find a useful list of engines at


This will be my last post for just over a week. I’m going on holiday to Canada tomorrow and will be back next week on 5th when I shall resume my regular (every other day) posts.


Category: programming Time: 2005-07-26 Views: 0

Related post

  • All About Smarty - The PHP Template Engine 2002-07-24

    Smarty is a PHP template engine written by Monte Ohrt and Andrei Zmievski. Yet another template engine you say? The authors (and I too) would disagree. Smarty is different from the rest of the pack. What differentiates Smarty from other template engi

  • Beyond The Template Engine 2003-09-18

    In general, template engines are a "good thing." I say this as a long time PHP/Perl programmer, user of many template engines (fastTemplate, Smarty, Perl's HTML::Template), and as author of my own, bTemplate. However, after some long discussions

  • Python template engines: What are the real benifits and drawbacks to XML vs custom syntax 2011-07-08

    I'm interested in knowing what are the real difference (benefits and drawbacks) between the two types of python templating engines; XML (like Genshi or Kid) and a custom syntax (like Cheetah or Jinja2). I'm not looking for which is better, or a recom

  • Should jQuery DOM manipulation be avoided in favor of JavaScript template engines? 2011-08-19

    From what I can tell, most developers will use heavy jQuery DOM manipulation tactics to deliver their updated AJAX views. Once things get out of hand, they may consider refactoring using template engines such as Mustache.js. Is this a valid assessmen

  • PHP Templating Engine 2012-01-11

    I need help refactoring code for my PHP template engine. I just have this feeling that the code is too disorganized and can be improved performance-wise. My main concerns are running a preg_match on every line in the file and hard-coding functionalit

  • How does the Drupal template engine work? 2012-03-19

    I've just started learning about Drupal 7's theme system. I need to know how the template engine handles php, html, and css inside its template files and how it interacts with Drupal core to make its final output onto the page. Basically, when we loa

  • 10 JavaScript and jQuery Templates Engines 2012-04-11

    A template is a way of specifying a function that produces a string in the output language (either in string or AST form) from a bundle of data using a syntax that is reminiscent of the output produced. Today we've come up with 10 JavaScript and jQue

  • Building a template engine - starting point 2012-06-29

    We're building a Django-based project with a template/generator component. This component will be separate from the project as such and can be Django/Python, Node, Java or whatever works. The idea is to generate questions based on a pattern. For eg.,

  • Spring MVC vs raw servlets and template engine? 2012-06-30

    Possible Duplicate: Why not Spring framework? I've read numerous articles about the Spring MVC framework, and I still can't see the benefits of using it. It looks like writing even a simple application with it requires creating a big hodgepodge of XM

  • Game Development at iOS+Android Platform : Template Engine for UI 2012-10-01

    We have broken down our game development process to UI Programming Core Game Programming Particle Effect Programming UI will be created by a group of programmers and those UI will be used at Core Game Programming. For example, at web applications, th

  • Save templates to file suddenly stopped working? 2013-04-19

    I have a clients site which has been working fine for a year or so, I have been doing a few updates to it over the last few days, now suddenly 'Saving Templates to File' has broken. If I make any changes to the templates via the control panel or via

  • Can I use the Twig template engine in Drupal 6/7? 2013-05-13

    I do a lot of Django development and I like their templating language. Twig, the new templating engine in Drupal 8, looks very much like it. How can I use Twig in Drupal 7 or even Drupal 6? --------------Solutions------------- Yes, you can, here is a

  • Wordpress as template engine 2013-07-25

    i would like to use Wordpress as template engine in my custom bootstrap/zurb foundation sites. I found there's lot of custom styles included in wordpress articles by default. Are there any ways how to get 'pure'(not formatted) content from wordpress

  • Custom PHP Template Engine 2013-07-31

    I've been developing a custom PHP template engine to suit just my needs and also to get a little more practice with PHP. What I did was to create a Template class that simply receives as constructor parameter a path to a template file and a string th

  • Basic templating engine in F# using F# syntax 2014-04-16

    I've been working on a rough idea for a templating engine (mostly as a learning project) using the F# syntax. How "correct" is my code in terms of being idiomatic F#? Are there F# features which would make the code simpler? More easily understoo

  • EE will not save templates as text files- No matter what 2014-04-17

    THE PROBLEM: When editing a template file via the control panel, saving it with "save template as text file" ticked gives the error: "Unable to save your template as a text file" The template is updated in the database, but not saved o

  • Refusing to use a common template engine 2014-06-27

    As the title says, I refuse to use a common template engine. I have my reasons, so instead of doing this: <ul> <li><a href = "#">test</a> <li>{CommonParam}</li> </ul> I do this: function listBlock($array

  • An Open-Source Java templating engine with inheritance 2014-10-21

    I'm developing a Java-servlet based project and I'm a newbie in this platform. I usually use Django, that has an awesome default template engine. Is there something similar for Java? PS: Template inheritance is a must (to prevent repetition). There a

  • will drupal 8 twig have inheritance like the old template engine? 2014-11-12

    Drupal 7 you can style down to the fields on the page will we have this type of control with twig? I have reviewed a lot of documents about twig and didn't see anything that lets you have that same inheritance as the Drupal 7 template engine. If this

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 0.834 (s). 13 q(s)