Section 3.2. Generating Output

Putting It All Together > Generating Output

3.2. 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:


template_dir

Defines the location of your templates. This must be the absolute path for the template directory.


compile_dir

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.


plugins_dir

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.


debugging

Enables the debugging console. The console is a pop up that displays debugging information related to the template being displayed.

It enables debugging for all the templates and requests. For a more controlled way of debugging, take a look at $debugging_ctrl.


debugging_ctrl

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.


compile_check

If it's set to true, every time a template is requested, Smarty checks whether it has changed since last compilation by comparing the timestamp of both.

By default it is set to true, so be sure turn it off on production sites, otherwise it will affect the performance.

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.


force_compile

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.


caching

Enables template caching. The cache lifetime is handled by the $cache_lifetime variable.


cache_dir

Defines the path to the directory where the cached templates will be stored.

This directory must be writable by the web server user.


cache_lifetime

Sets the cache lifetime in seconds.


cache_handler_func

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.[double dagger]

[double dagger] There are many different cache solutions available either as open source or commercially. memcached is a distributed solution developed by Danga Interactive that stores cache objects in memory (http://www.danga.com/memcached/).


cache_modified_check

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).


default_template_handler_func

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.


security

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.


compile_id

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.

 

 



PHP and Smarty on Large-Scale Web Development
PHP and Smarty on Large-Scale Web Development
ISBN: 047008023X
EAN: N/A
Year: 2007
Pages: 20
BUY ON AMAZON

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net