Chapter 6. Smarty API

Table of Contents
Variables
Methods

Variables

$template_dir

This is the name of the default template directory. If you do not supply a resource type when including files, they will be found here. By default this is "./templates", meaning that it will look for the templates directory in the same directory as the executing php script.

Technical Note: It is not mandatory to put this directory under the web server document root.

$compile_dir

This is the name of the directory where compiled templates are located. By default this is "./templates_c", meaning that it will look for the compile directory in the same directory as the executing php script. This was added to Smarty version 1.2.1.

Technical Note: This setting must be either a relative or absolute path. include_path is not used for writing files.

Technical Note: It is not mandatory to put this directory under the web server document root.

$config_dir

This is the directory used to store config files used in the templates. Default is "./configs", meaning that it will look for the configs directory in the same directory as the executing php script.

Technical Note: It is not mandatory to put this directory under the web server document root.

$plugins_dir

This is the directory where Smarty will look for the plugins that it needs. The directory must be relative to the directory where Smarty itself is installed. Default is "plugins". There can be only one plugins directory.

$debugging

This enables the debugging console. The console is a javascript window that informs you of the included templates and assigned variables for the current template page.

NOTE: This was added to Smarty 1.4.3.

$debug_tpl

This is the name of the template file used for the debugging console.

NOTE: This was added to Smarty 1.4.3.

$debugging_ctrl

This allows alternate ways to enable debugging. NONE means no alternate methods are allowed. URL means when the keyword SMARTY_DEBUG is found in the QUERY_STRING, debugging is enabled for that invocation of the script. If $debugging is true, this value is ignored.

NOTE: This was added to Smarty 1.4.4.

$global_assign

This is a list of variables that are always implicitly assigned to the template engine. This is handy for making global variables or server variables available to all templates without having to manually assign them. Each element in the $global_assign should be either a name of the global variable, or a key/value pair, where the key is the name of the global array and the value is the array of variables to be assigned from that global array. $SCRIPT_NAME is globally assigned by default from $HTTP_SERVER_VARS.

Technical Note: Server variables can be accessed through the $smarty variable, such as {$smarty.server.SCRIPT_NAME}. See the section on the $smarty variable.

$undefined

This sets the value of $undefined for Smarty, default is null. Currently this is only used to set undefined variables in $global_assign to a default value.

$compile_check

Upon each invocation of the PHP application, Smarty tests to see if the current template has changed (later time stamp) since the last time it was compiled. If it has changed, it recompiles that template. As of 1.4.0, if the template has not been compiled, it will compile regardless of this setting. By default this variable is set to true. Once an application is put into production (templates won't be changing), the compile_check step is no longer needed. Be sure to set $compile_check to "false" to improve performance! Note that if you change this to "false" and a template file is changed, you will *not* see the change since the template will not get recompiled. If caching is enabled and compile_check is enabled, then the cache files will get regenerated if an involved template file was updated. See $force_compile or clear_compiled_tpl.

$force_compile

This forces Smarty to (re)compile templates on every invocation. This setting overrides $compile_check. By default this is disabled. This is handy for development and debugging. It should never be used in a production environment. If caching is enabled, the cache file(s) will be regenerated every time.

$caching

This tells Smarty whether or not to cache the output of the templates. By default this is set to false. If your templates generate redundant redundant content over and over again and again repeatedly, it is advisable to turn on caching. This will result in significant performance gains. You can also have multiple caches for the same template. See is_cached for details. This was added to Smarty 1.3.0.

If $compile_check is enabled, the cached content will be regenerated if any of the involved templates are changed. (new to 1.4.6). If $force_compile is enabled, the cached content will always be regenerated. (added to Smarty 1.4.7)

$cache_dir

This is the name of the directory where template caches are stored. By default this is "./cache", meaning that it will look for the cache directory in the same directory as the executing php script. You can also use your own custom cache handler function to control cache files, which will ignore this setting. (added to Smarty 1.3.0)

Technical Note: This setting must be either a relative or absolute path. include_path is not used for writing files.

Technical Note: It is not mandatory to put this directory under the web server document root.

$cache_lifetime

This is the length of time in seconds that a template cache is valid. Once this time has expired, the cache will be regenerated. $caching must be set to "true" for this setting to work. You can also force the cache to expire with clear_all_cache. (added to Smarty 1.3.0)

$cache_handler_func

You can supply a custom function to handle cache files instead of using the built-in method using the $cache_dir. See the custom cache handler function section for details.

$cache_modified_check

If set to true, Smarty will respect the If-Modified-Since header sent from the client. If the cached file timestamp has not changed since the last visit, then a "304 Not Modified" header will be sent instead of the content. This works only on cached content without insert tags.

$default_template_handler_func

This function is called when a template cannot be obtained from its resource. (added to Smarty 1.5.2)

$php_handling

This tells Smarty how to handle PHP code embedded in the tempalates. There are four possible settings, default being SMARTY_PHP_PASSTHRU. Note that this does NOT affect php code within {php}{/php} tags in the template.

  • SMARTY_PHP_PASSTHRU - Smarty echos tags as-is.

  • SMARTY_PHP_QUOTE - Smarty quotes the tags as html entities.

  • SMARTY_PHP_REMOVE - Smarty removes the tags from the templates.

  • SMARTY_PHP_ALLOW - Smarty will execute the tags as PHP code.

NOTE: Embedding PHP code into templates is highly discouraged. Use custom functions or modifiers instead.

$security

$security true/false, default is false. Security is good for situations when you have untrusted parties editing the templates (via ftp for example) and you want to reduce the risk of system security compromises through the template language. Turning on security enforces the following rules to the template language, unless specifially overridden with $security_settings:

  • If $php_handling is set to SMARTY_PHP_ALLOW, this is implicitly changed to SMARTY_PHP_PASSTHRU

  • PHP functions are not allowed in IF statements, except those specified in the $security_settings

  • templates can only be included from directories listed in the $secure_dir array

  • local files can only be fetched from directories listed in the $secure_dir array using {fetch}

  • {php}{/php} tags are not allowed

  • PHP functions are not allowed as modifiers, except those specified in the $security_settings

NOTE: Security features were added to Smarty 1.4.3.

$secure_dir

This is an array of all local directories that are considered secure. {include} and {fetch} use this when security is enabled.

$security_settings

These are used to override or specify the security settings when security is enabled. These are the possible settings:

  • PHP_HANDLING - true/false. If set to true, the $php_handling setting is not checked for security.

  • IF_FUNCS - This is an array of the names of permitted PHP functions in IF statements.

  • INCLUDE_ANY - true/false. If set to true, any template can be included from the file system, regardless of the $secure_dir list.

  • PHP_TAGS - true/false. If set to true, {php}{/php} tags are permitted in the templates.

  • MODIFIER_FUNCS - This is an array of the names of permitted PHP functions used as variable modifiers.

$trusted_dir

$trusted_dir is only for use when $security is enabled. This is an array of all directories that are considered trusted. Trusted directories are where you keep php scripts that are executed directly from the templates with {include_php}.

$left_delimiter

This is the left delimiter used by the template language. Default is "{".

$right_delimiter

This is the right delimiter used by the template language. Default is "}".

$show_info_header

Shows an HTML comment at the beginning of the templates output, displaying smarty version and date generated. Default is false.

$show_info_include

Shows an HTML comment before and after each included template. Default is false.

$compiler_class

Specifies the name of the compiler class that Smarty will use to compile the templates. The default is 'Smarty_Compiler'. For advanced users only.

$request_vars_order

The order in which request variables are registered, similar to variables_order in php.ini

$compile_id

Persistant compile identifier. As an alternative to passing the same compile_id to each and every function call, you can set this compile_id and it will be used implicitly thereafter.