PHP smarty Template Use Method Example detailed _php instance

Source: Internet
Author: User
In this paper, the use of Smarty template is analyzed in detail, which has a 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 the Assign function in the template to assign values *}><{$contacts [row].phone}> <{* array type variables, You need to call the Assign function within the template to assign a value *}> <{* the value of the variable read from the configuration file and output *}>

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 ( In fact, they will actually be replaced by this.

C . Output the variables assigned from PHP in the template

There are two types of variables that are used frequently in Smarty templates: One is a variable assigned from PHP, and the other is a variable read from a configuration file
Note: The template can only output variables allocated from PHP, not in the template for these variables to be reassigned, the variable is the whole domain, as long as the allocation once, if allocated more than two times, the variable content will be the final allocation of the main
The Foreach or section statements provided in Smarty are used to iterate through each element in the output array, and the index array and the associative array are exported slightly differently in the template, and the indexed array is accessed in the template in the same way as the reference in the PHP script. The way an element in an associative array is specified in a template is using '. ' Access to.
Access to the object in the template is the same as in the PHP script, which is done by the operator, "--".

Iv.Mathematical calculation of variables in Smarty template

Variables in the template cannot be assigned directly, 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}> <{* Compound type variable participates in the Operation *}><{if ($foo + 2 = =)}> <{* mathematical operations applied in program logic *}>

Variables embedded in double quotation marks can be identified in the Smarty template, but some variables must be wrapped in an inverse quotation mark "(This symbol and ' ~ ' on the same key), as shown below:

<{"Test $foo test"}> <{* double quotes use variable *}><{"test" $foo [0] ' test '}> <{* double quotes enclose the array variable in the inverted quotation mark *}><{"Test ' $foo. Bar ' test '}> <{*-Enclosed object member variable in double quotation marks *}>

Five,smarty template use example :

1th Step: Loading 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: Use the display () method of the Smarty object to output the template content

Assign () Method:

This method is used to assign values to variables in the template, which is easier to use
Prototype: void assign (String varname,mixed var)
This method can assign the type data supported by PHP to the template variable containing the array and the object
There are two ways to use them:

Specify a pair of ' name/value ' $smarty->assign (' question ', ' hello '), $smarty->assign (' answer ', ' not good ');//Specify a ' name/value ' $smarty- >assign (' question ' = ' How are you ', ' answer ' = ' not good ');//This is a less-used approach

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 Smarty.class.php file        define (' Site_root ', './tpl/');//define a constant $TPL = New Smarty (); $tpl->template_dir = Site_root. ' Template_dir ';//Save template file  $tpl->compile_dir = site_root. ' Compile_dir ';//save compiled template file $tpl->config_dir = Site_root. ' Config_dir ';//Save special configuration file $tpl->cache_dir = Site_root. ' Cache_dir ';//save Smarty Cache file  $tpl->caching = 1;//Enable cache $tpl->cache_lifetime = 60*60*24;//cache time 1 days  $TPL Left_delimiter = ' <{';//left Terminator $tpl->right_delimiter = '}> ';//Right Terminator

7, index.php file home code as follows

<?php       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

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