The $smarty Reserved Variable
Smarty has one special variable, $smarty, for accessing configuration variables, PHP's superglobals, and so on.
To access a Superglobal through $smarty, use $smarty.superglobal_name.key. For example, a page_id value passed in the URL would be accessible in $smarty.get.page_id and $_SESSION['name'] can be accessed as $smarty.session.name.
Constants in PHP code are available through $smarty.const. The syntax is similar to that of accessing Superglobals: $smarty.const.CONSTANT_NAME.
$smarty.now represents the current timestamp, useful in formatting dates. (The sample Smarty template that NuSphere's PhpED uses makes reference to this variable, see the image>)
Configuration variables (see below) are accessible through $smarty.config.
The current template value is stored in $smarty.template (this can be useful for debugging).
The current version of Smarty is stored in $smarty.version.
A couple of functions make use of $smarty, as well. The "section" and "foreach" loops can use $smarty.section and $smarty.foreach.
Configuration Files
Smarty supports application configuration files, where you can store useful values that the templates may require. A configuration file is a plain text file, normally given a .conf extension. Within the file, use a name=value syntax:
some_thing = "This value"
default_color = "#FFFFFF"
Note that this is not PHP code, so the names do not start with a dollar sign and the lines do not end with semicolons. You do not even have to quote the values, although doing so makes stylistic sense.
Comments can be placed within a configuration file using almost any syntax, but the recommendation is to precede the comment with the hash:
# this is a comment.
Configuration files can be broken into sections, identified by a name within square brackets:
some_thing = "This value"
default_color = "#FFFFFF"
[some_section]
some_other_thing = "some other value"
Values defined outside of a section are considered global. This distinction is relevant when loading configuration files in a template. If the same name is given the same value globally and within a section, the section value will overwrite the global one.
Configuration files can be loaded into a template using the "config_load" function:
{config_load file='somefile.conf'}
To load only a specific section of the config file, follow the configuration file name with the section:
{config_load file='somefile.conf' section='some_section'}
When only a section of a configuration file is loaded, that section's variables are loaded, as well as any global variables.
Alternatively, you can load a configuration file using the config_load() method:
require('Smarty.class.php');
$page = new Smarty();
$page->config_load('somefile.conf');
$page->config_load('somefile.conf', 'some_section');
Within a template, the config variables use special notation: #var_name#. Once a configuration file, or a section of it, is loaded, any placeholder in the template in the form of #var_name# will be replaced with the corresponding value from the configuration file.
|