Share the use of PHP's Smarty template.
This paper analyzes the use of Smarty template, and has certain reference value for learning Smarty friends.
Details are as follows:
I. Comments in the template
Each smarty template file is developed using the syntax of the web foreground language (xhtml,css, JavaScript, etc.) combined with the Smarty engine.
The web foreground is used to develop a language that is exactly the same as the original, and the annotations do not change.
Smarty comment syntax is ' left terminator variable value * ' and ' * Right terminator variable value ', the content between these two delimiters is a comment content, can contain one or more lines, and users browse the Web page to view the original code without seeing the comment, it is only the template intrinsic comments, the following is an example of comments.
$smarty->left_lelimiter = ' <{';
$smarty->right_delimiter = '}> ';
Comments:
<{* this a note *}>
Second, the variable declaration in the template
In Smarty, everything is dominated by variables, and all rendering logic gives the template its own control. There are several different types of variables, and the type of the variable depends on what symbol it is prefixed with or what symbol it is surrounded by. Variables can be directly output or parameters of job function properties and modifiers or for internal conditional expressions, etc.
<{$name}> <{* general type variables, you need to call assign function assignment values within the template *}>
<{$contacts [row].phone}> <{* Array type variables, you need to call assign function assignment values within the template *}>
<body bgcolor= "<{#bgcolor #}>" > <{* the value of the variable read from the configuration file and outputs *}>
If you output a variable assigned from PHP in the Smarty template, you need to precede the variable with a $ symbol and enclose it in a delimiter, named in the same way as a PHP variable, and the bounding notation is somewhat like the <?php?> in PHP ( As a matter of fact, they are actually replaced by this one, which is the focus of much of the PHP tutorial, as you'll notice.
Iii. output variables assigned from PHP in the template
the variables that are frequently used in the Smarty template are the variables assigned from PHP, and the variables read from the configuration file
Note: Only variables allocated from PHP can be exported from the template. These variables cannot be reassigned in the template, the variables are all domain, as long as they are allocated once, and if more than two times are allocated, the variable content is used to iterate through each element in the output array with the last allocated foreach or section statement in the main
Smarty. indexed arrays and associative arrays are exported in a slightly different way from the template, and the indexed array is accessed in the template in the same way as the reference in the PHP script, and the elements in the associative array are specified in the template using '. ' Access to. The
access to the object in the template is the same as in the PHP script, which is done through the operator, '--.
Iv. Mathematical calculation of variables in Smarty templates
variables in the template cannot be directly assigned, but can participate in mathematical operations, as long as the mathematical operations that can be performed in a PHP script can be applied in the template, as follows:
<{$foo + 1}> < {* variable plus 1 *}>
<{$foo * $bar}> <{* two variables multiplied *}>
<{$foo->bar-$bar [1] * $foo->bar-3 * 7}> &L T {* Compound type variable participates in operation *}>
<{if ($foo + 2 = =)}> <{* Mathematical operation applies *}> in program logic;
The
can recognize variables embedded in double quotation marks in the Smarty template, but some variables must be wrapped with an inverse quotation mark ' (This symbol and ' ~ ' on the same key), as follows:
<{"Test $foo test"}> <{* double quotes using variable *}
<{"Test" $foo [0] ' test '}> <{* double quotation marks enclose the array variable *}>
<{"test" $foo. Bar ' test '}> <{* *}> object member variable enclosed in double quotation marks;
v. Smarty template Use Example:
Step 1th: Load the Smarty template engine, such as: Require ' libs/smarty.class.php '
2nd step: Create a Smarty object, such as: $smarty = new Smarty ();
3rd Step: Modify Smarty default behavior, such as: open cache, template storage path, etc.
4th step: Assign the data obtained in the program to the corresponding variable in the template through the Assign () method of the Smarty object
5th step: Display with the Smarty object The () method outputs the template content to the
Assign () Method: (Script Academy Www.jbxue.com)
This method is used to assign values to variables in the template, using a relatively easy
prototype: void Assign (String varname,mixed var)
This method can be used to assign the type data supported by PHP to the template variable containing arrays and objects
in the following two ways:
//Specify a pair of ' name/value '
$smarty->assign (' question ', ' How are you '? );
$smarty->assign (' answer ', ' not good ');
//Specify a ' name/value '
$smarty->assign (' question ' = ' How are you ', ' answer ' = ' not good '));//This method is less used
Display () Method:
This method must be used in a smarty-based script, and can only be used once in a script, and it is responsible for getting and displaying the template referenced by the Smarty engine
Prototype: var display (string template[,string cache_id][,string compile_id])
Parameter one: template is required and specifies the type and path of a legitimate template resource
Parameter two: cache_id specifies the name of a cache identifier
Parameter three: COMPILE_ID used when maintaining multiple caches on a page
Use the following method
$smarty->display (' tpl/template_dir/template.html ');
Simple example:
1, Libs: is the Smarty class library
2. Tpl/cache_dir: Store Cache template
3, Tpl/compile_dir: Store the post-compilation template file
4. Tpl/config_dir: Store Special configuration Files
5. Tpl/template_dir: Store template file
6, smarty.php file in the new out of a Smarty class object, and set the property values of each object, the following code
<?PHPrequire' libs/smarty.class.php ';//load the Smarty.class.php fileDefine(' Site_root ', './tpl/');//Define a constant$TPL=NewSmarty ();//Www.jbxue.com$TPL->template_dir = Site_root. ' Template_dir ';//Save template file$TPL->compile_dir = Site_root. ' Compile_dir ';//to save a compiled template file$TPL->config_dir = Site_root. ' Config_dir ';//Save a special configuration file$TPL->cache_dir = Site_root. ' Cache_dir ';//Save Smarty Cache file$TPL->caching = 1;//Enable caching$TPL->cache_lifetime = 60*60*24;//Cache time 1 days$TPL->left_delimiter = ' <{';//left End character$TPL->right_delimiter = '}> ';//Right End character
7, index.php file home code as follows
<? require ' smarty.php '; $tpl->assign (' title ', ' title Test '); $tpl->assign (' content ', ' content test '); $tpl->display (' template.html '); 8, Tpl/template_dir/template. HTML This is a template file code as follows $title}></title>$ Content}></body>