Putting It All Together > Generating Output
As described previously, all output is generated by the Smarty Template Engine.
Having a flexible configuration, Smarty can be used in a lot of different ways. You can change Smarty configuration settings by manipulating some attributes of the Smarty class.
Here's a list of the most common options that you need to change:
Defines the location of your templates. This must be the absolute
Defines the location where Smarty will store the compiled templates. The value must be the absolute path to a directory that is writable by the web server. It's a good idea to keep it out of your web server document root.
Accepts an array of paths from which Smarty will search for the required plugins.
Although Smarty supports relative paths for the plug-ins, you can get better performance using absolute paths. This is very useful to keep your plug-ins organized.
Enables the debugging console. The console is a pop up that displays debugging information
It enables debugging for all the templates and
If it's set to URL , using the SMARTY_DEBUG keyword as a GET variable will enable the debugging console.
If $debugging is set to true , this has no effect.
If it's set to
, every time a template is
By default it is set to
, so be sure
If you update the templates often, the best way to be sure the new template is compiled is to delete the old compiled one, which you can find in $compile_dir ; this will force Smarty to compile it regardless of the $compile_check value.
If set to true , it will force the template compilation on every request.
This can be useful in the developing environment, but it must be disabled in production, because it affects overall performance.
Enables template caching. The cache lifetime is handled by the $cache_lifetime variable.
Defines the path to the directory where the cached templates will be stored.
This directory must be writable by the web server
Sets the cache lifetime in seconds.
It can be set to a cache handling function.
This is very useful if you don't want to store the cache in the filesystem, or if you want to use another cache system, like memcached. [ ]
[ ] There are many different cache solutions available either as
opensource or commercially. memcached is a distributed solution developed by Danga Interactive that stores cache objects in memory (http://www.danga.com/memcached/).
Tells Smarty to respect the If-Modified-Since HTTP header. [ §] This is a very good way to save bandwidth.
[ §] This header is mostly used to serve only documents that have not been modified for a given period of time. For more information, see "RFC2616 ”HTTP/1.1: Header Field Definitions," §14.25 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html).
If Smarty can't find the template, it will call the function provided in this variable.
This is very useful to handle a blend of default and customized templates.
This is a very useful setting if you want your designers to have access to the production environment, or you want to let your users change templates.
If you have different versions of the same template, you can use the $compile_id variable to identify them.
Let's take a look at an example script that makes use of some of the previous Smarty options. Notice that the entire configuration-based values are obtained from the $conf array (we'll provide more information on how to set it up later on):
<?php // Load the Smarty library. require_once 'lib/smarty/Smarty.class.php'; // Load configuration values. require_once 'config/.php/smarty.php'; // Instantiate a Smarty object. $smarty = new Smarty; // Configure the Smarty template directory. $smarty->template_dir = $conf['smarty']['paths']['template']; // Configure the Smarty compile directory. $smarty->compile_dir = $conf['smarty']['paths']['compile']; // Configure the Smarty cache directory. $smarty->cache_dir = $conf['smarty']['paths']['cache']; // Configure the Smarty compile check flag. $smarty->compile_check = $conf['smarty']['compile']['ckeck']; // Configure the Smarty compile forcing flag. $smarty->force_compile = $conf['smarty']['compile']['force']; // Configure the Smarty caching flag. $smarty->caching = $conf['smarty']['caching']; // Start the debugging console whenever the 'SMARTY_DEBUG' GET parameter is found. $smarty->debugging_ctrl = $conf['smarty']['allowdebug'] ? 'URL' : 'NONE'; ?>
After this, register whichever objects or functions you want to use inside the Smarty templates and, of course, display the appropriate template that generates all the output.