PHP smarty Common Variable Learning notes

Source: Internet
Author: User
Tags arrays modifier modifiers php code php source code set set


Now the development of the site to use the Smarty framework template, feel very good, better realize the PHP source code and the separation of HTML. Now share some common variables of smarty.

1, smarty_dir SMARTY directory

Locate the full system path of the Smarty class file, and if no smarty directory is defined, Smarty will try to automatically create the appropriate value. If defined, the path must end with a slash.

Define ("Smarty_dir", "/usr/local/lib/php/smarty/");

Require_once (Smarty_dir.) Smarty.class.php ");

2, $template _dir template directory variables

The variable defines the name of the default template directory. When you include a file, if you do not provide a source type (that is, the source address), you will find it in the template directory. By default, the directory is: "./templates", which means that he will look for the template directory in the same directory as the PHP execution script.

3, $compile _dir compile directory variables

This variable locates the directory name of the compiled template. By default, the directory is: "./templates_c", which means that he will look for the compiled directory in the same directory as the PHP execution script.

4, $config _dir configuration directory variables

The variable defines the directory used to hold the template configuration file, by default, the directory is: "./configs", which means that he will look for the configuration directory in the same directory as the PHP execution script.

5, $plugins _dir plug-in directory variables

The variable defines the directory where the Smarty is looking for the desired plug-in. The default is the "plugins" directory in the Smarty directory. If a relative path is provided, Smarty will first look in the Smarty directory, then look in the current working directory, and then search through each path in the PHP inclusion path.

6, $debugging Debugging variables

It can start the debug console. The console is a JavaScript window that tells you which templates are included and variables that apply to the current template page.

7, $debug _tpl Debug Template

This variable defines the template file name used to debug the console. By default, its name is: DEBUG.TPL, located in the Smarty directory.

8, $debugging _ctrl debugging control variables

The variable function allows you to start debugging settings alternately. None means that alternate methods are not allowed. The URL indicates that when the keyword Smarty_debug appears in the Query_string (query string), debugging is initiated for the invocation of this script. If the $debugging variable is true, its debug control variable value is ignored.

9. $global _assign Global configuration variables

Defines a list of variables that are always implicitly used for the template engine. This is convenient for making global variables or server variables available to all templates because you do not have to configure them manually. Each element in the global configuration should be either the name of a global variable or a key/value pair (the key is a global array name and the value is an array of variables that should be global arrays). $SCRIPT _name variable defaults to the $http_server_vars array.

10, $undefined undefined variable

This variable sets the value for the Smarty variable that is not defined, and the default is null. Currently, only the undefined variable in the global configuration is set to a default value.

11, $autoload _filters automatic load filter variable

If you want to load filters in each template call, you can specify that they use this variable, and Smarty will automatically load them for you. The variable is a federated array in which the key is a filter type and the value is an array of filter names. For example:

$smarty->autoload_filters = Array (' Pre ' => array (' trim ', ' stamp '),

' Output ' => array (' convert ');

12, $compile _check compile check variable

Each time the PHP application is invoked, Smarty will try to see if the current template has been modified since the last compilation time. If it has been modified, she'll recompile the template. If the template has not been compiled, she will compile the template regardless of how the compilation check is set Set. Compile-Check this variable to true by default. Once an application is put into the product (the template will not be modified), the compile check is no longer required. For maximum performance, determine $compile_check set to false. Note: If set to False , although the template file is modified, you will not see the result of the modification because the template has not been recompiled. If you start a cache and compile check, the cached files will be rebuilt once the template file or configuration file is updated.

13, $force _compile forced to compile variables

Force Smarty to compile the template every time it is invoked (re). This setting is not subject to $compile_check. It is not valid by default. It is convenient for development and debugging. But it must never be used in a product environment. If caching is started, the cached file will be regenerated each time.

14, $caching Cache variables

Tells Smarty whether to cache the output of the template. By default, she is set to 0, or invalid. If the template produces redundant content, it is recommended that caching be turned on. This is beneficial to obtain good performance gain. You can also have multiple caches for the same template. 1 tells Smarty to use the current $ The Cache_lifetime variable determines whether the cache expires. 2 tells Smarty to use the Cache_lifetime value when generating the cache. In this way you can set the cache lifetime before you get the template to control exactly when the cache expires.

If a compilation check is started, the cached content is regenerated once any template file or configuration file (configuration file for the cache portion) is modified. If forced compilation is started, the cached content will always be regenerated.


Php-smarty Some common variables in the second chapter.

15, $cache _dir cache directory variables

This is the directory name where the template cache is stored. By default, it is: "./cache", which means you can look for a cached directory in the same directory as the PHP execution script. You can also use your own custom cache handler to control the cached file, which will ignore this setting.

16, $cache _lifetime cache survival time variable

The variable defines the length of the template cache valid time period (in seconds), and once that time expires, the cache is regenerated. If you want to achieve all effects, $caching must be set to "true" for $cache_lifetime needs. When the value is-1, Will force the cache to never expire. A value of 0 will cause the cache to always be regenerated (only for testing, a more efficient way to invalidate the cache is to set $caching = False.)

If forced compilation is started, the cached file is regenerated each time. To effectively stop caching, you can use the Clear_all_cache () function to clear all cached files, or use the Clear_cache () function to purge individual files (or filegroups).

17, $cache _handler_func cache processing function variables

You can provide a custom function to work with the cached file instead of using the built-in method via the variable $cache_dir. Xiang See: Cache handler function section

18, $cache _modified_check cache modification Check Variable

If you set this variable to True, Smarty analyzes the If-modified-since header information sent by the client. If the cached file timestamp has not changed since the last visit, a "304 not Modified" header is sent. Instead of caching the contents of the file. This approach works only with cached content that does not have the {insert} tag.

19, $config _overwrite configuration override variable

If you set this variable to be true, the variables read from the configuration file will overwrite each other. Otherwise, the variable will be placed in an array. This is useful if you want to store the data in the configuration file in an array, just listing each element more than once. By default, set to True.

20, $config _booleanize configuration of Boolean variables

If the variable is set to True, the On/true/yes and off/false/no values in the configuration file are automatically converted to a Boolean value. In this case, you can use these values in the template like {if #foobar #}pp815 {/if}. If Foobar is on, True or Yes, the {if} statement executes. By default, the value of this variable is true.

21, $config _read_hidden configuration read hidden variables

If set to True, the hidden section blocks in the configuration file (which are not translated by HU) can be read from the template. Typically you will be set to fake so that you can store sensitive data, such as database parameters, in the configuration file without worrying about the template calling them. By default, the variable is set to False.

22, $config _fix_newlines to configure the fixed line feed variable

If the variable is set to True, the Mac and DOS newline characters (R and RN) in the configuration file will be converted to n when parsing. By default, this variable is true.

23, $default _template_handler_func Default template processing function variables

The function is invoked when the template cannot be fetched from its source directory.

24, $php _handling PHP processing variables

This variable tells Smarty how to handle the PHP code embedded in the template. There are four possible settings, the default is Smarty_php_passthru. Note: The change in the amount of the settings will not affect the templates inside the {php}{/php} tags in the PHP code.

Output markup as is.

As an HTML entity reference token

Remove the tag from the template.

Tags will be executed as PHP code.

Extremely disapprove of embedding PHP code in a template.

25, $security Security variables

Security variables are either True or FALSE. Default is False. When you do not trust the editable parts of the template (for example, uploading edits via FTP), and if you want to reduce the risk of system insecurity through the template language, the security variables are set to be really appropriate. Setting to true imposes the following rules on the template language, unless you specifically use the $ Security_settings covered.

If the variable $php_handling is set to Smarty_php_allow, it is implicitly modified to Smarty_php_passthru
PHP functions are not allowed in an if statement, except as otherwise noted in $security_settings.
Templates can only be included in the directories listed in the $secure_dir array.
Local files can only be obtained from {fetch} in the directories listed in the $secure_dir array.
{PHP} {/php} tags are not allowed
{php}{/php} tag is not allowed.

PHP functions are not allowed as modifiers except in $security_settings.


PHP smarty commonly used in the development of some common variables (c), smarty Programmer should know the variables.

26, $secure _dir Security directory variables

This is a security-related local directory array variable. This array variable is used by {include} and {fetch} when the security variable is started.

27, $security _settings Security Configuration variables

These are used to override or specify the ' Security settings ' is enabled. These are the possible settings:
Used to override or otherwise specify a security configuration when starting a security variable. There are several possible configurations:

php_handling– true or False. If true, the $php_handling configuration is not checked.
if_funcs– This is a PHP function array group that is allowed to be included in the IF statement.
include_any– true or False. If true, you can include any template from the file system, regardless of the $secure_dir directory settings.
php_tags– true or False. If true, {php}{/php} is allowed to exist in the template.
modifier_funcs– This is a PHP function array group that can be used as a modifier.
28, $trusted _dir Trust directory variables

Trust directory variables are only used after $security startup. This is an array of all trusted directory arrays. You can place PHP scripts in these trust directories, which are executed directly in the template with the {include_php} tag.

29. $left _delimiter left Terminator variable

In the template language, the default is "{".

30, $right _delimiter right terminator variable

Used in the template language, the default is "}".

31, $compiler _class compiler class variables

Specifies the name of the compiled class used by Smarty to compile the template. Default is: ' Smarty_compiler '. For advanced users only.

32, $request _vars_order variable order variable

The order of the request variables is configured here, similar to the order of variables in php.ini.

33, $request _use_auto_globals automatic global variable

Specifies whether Smarty uses PHP's $http_*_vars[] array variable (default $request_use_auto_globals=false) or $_*[] Array ($request _use_auto_globals=true) Variable. This has an effect on templates that use {$smarty. request.*}, {$smarty. get.*}, and so on. It's worth noting that if you set $request_use_auto_globals as true, Variable.request.vars.order is not valid, but PHP's Gpc_order configuration value also works.

34. $compile _id Compile ID variable

A permanent compilation identification number. As an option to pass the same compilation number to each function call, you can set the compilation ID, which will then be implicitly used.

35, $use _sub_dirs of subdirectory variables

If your PHP environment does not allow Smarty to create subdirectories, set this variable to be false. Subdirectories are very useful, so use them as much as possible.

36. $default _modifiers Default modifier variable

This is a modifier array variable, these modifiers are implicitly used for each variable in the template. For example: For HTML, an array (' Escape: ' Htmlall ') ignores each variable by default; For a variable to be exempt from the default modifier, use the argument "Nodefaults" Special "Smarty" modifier for it, for example: {$var |smarty:nodefaults}.

37. $default _resource_type Default Source type variable

The variable tells Smarty what source type to use implicitly. The default value is ' file ', that is, $smarty->display (' Pp815/index.tpl '); and $smarty->display (' File:index.tpl '); from the meaning is the same.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.