Smarty Template Engine for PHP
Smarty is the more or less official template engine for PHP. http://smarty.php.net/
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/ configs/ templates_c/ templates/
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); $smarty->is_cached(...); $smarty->clear_cache(...);
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.
--gullevek 03:27, 24 May 2006 (CEST)