From AdminWiki

Revision as of 01:49, 24 May 2006 by Gullevek (Talk | contribs)
Jump to: navigation, search


Smarty Template Engine for PHP

Smarty is the more or less official template engine for PHP.


Smarty is very easy to use and very powerful. It gives you the most esiest way to fully seperate code from html - which you should ALWAYS do.


  • Download Smarty and unpack it
  • you need only the "libs" folder.

I recommend you rename and call it something like "Smarty-<version>" and create a symlink to it with just the name "Smarty"

  • create the following directories:

cache is the place where smarty keeps cached pages if the feature is turned on. configs holds config include files for smarty. templates_c is the compile directory for smarty. templates is the directory for holding the templates. cache and templates_c need to be writeable by the web server.

  • thats it you are done and can start using smarty.

Tips and Tricks

How to use compile & caching if you have more than one template set.

I have certain projects where I have more than one template set. So in the default setting of smarty I get a caching and compile problem, because of the same template name smarty thinks the page has already been compiled. To avoid this use the additionl function parameters.

 $smarty->display($template, $cache_id, $compile_id);

with adding $cache_id and $compile_id you can make the template unique in the templates_c directory.

I recommend to add page ids, or other unique sets to the cache_id to make the template unique for the cache. Important if one template has several languages or different content depending on the same id. Remember the cached file will be used whenever display finds a match.

For the compile_id I would add the complete template path. I have subdirectories in the templates directory, so I add the full template path here. Do not worry about special characters, smarty escapes them.

Extensions to Smarty

some extensions to smarty. Can be addition functions, blocks or other extensions.

used gettext for language translation in smarty

to be done by gullevek (perhaps on extra page):

  • smarty extend class code
  • t block code
  • extra classed needed
  • examples

add array [] to the html_checkboxes

if you want to send several checkboxes as an array you can simply add [] to the name. It works in smarty unless you need to have a value inside. So I wrote a small patch for the function.html_checkboxes file

link to the patch

the patch adds a new element in the {html_checkboxes} smarty element called pos. The value given is an array which values are used as values inbetween [].

--gullevek 03:27, 24 May 2006 (CEST)

Personal tools