How to use php Smarty templates _php Tutorial

Source: Internet
Author: User
Tags smarty template
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 developed in the same language 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 a small example of annotations.


$smarty->left_lelimiter = ' <{';

$smarty->right_delimiter = '}> ';

Note: <{* This a note *}>

The code is as follows Copy Code

$smarty->left_lelimiter = ' <{';

$smarty->right_delimiter = '}> ';

Note: <{* This a note *}>

Two: variable declaration in template


In Smarty, all variables are dominated, and all rendering logic allows the template to control itself.


There are several different types of variables, 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 *}>


<{* the value of the variable read from the configuration file and outputs the *}>


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 a bit like PHP (in fact, they are actually replaced with this).


Three: Output 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, "--".


Four: 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 JOIN operation *}>


<{if ($foo + 2 = = ten)}> <{* mathematical operations applied in program logic *}>

The code is as follows Copy Code

<{$foo + 1}> <{* variable plus 1 *}>


<{$foo * $bar}> <{* two variables multiplied *}>


<{$foo->bar-$bar [1] * $foo->bar-3 * 7}> <{* Compound type variable JOIN operation *}>


<{if ($foo + 2 = = ten)}> <{* 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:

The code is as follows Copy Code


<{"Test $foo test"}> <{* double quotes with variable *}>


<{"Test ' $foo [0] ' test '}> <{* double quotes enclose the array variable in inverted quotation marks *}>


<{"Test" $foo. Bar ' test '}> <{* double quotation marks enclose an object member variable in an inverted quotation mark *}>


<{"Test $foo test"}> <{* double quotes with variable *}>


<{"Test ' $foo [0] ' test '}> <{* double quotes enclose the array variable in inverted quotation marks *}>


<{"Test" $foo. Bar ' test '}> <{* double quotation marks enclose an object member variable in an inverted quotation mark *}>

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

Specify a pair of ' name/value '
$smarty->assign (' question ', ' How are you ');
$smarty->assign (' answer ', ' not good ');


Specify include ' name/value '
$smarty->assign (' question ' = ' How are you ', ' answer ' = ' not good ');//This is less
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


Require ' 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 caching


$TPL->cache_lifetime = 60*60*24;//cache time 1 days


$tpl->left_delimiter = ' <{';//left Terminator


$tpl->right_delimiter = '}> ';//Right Terminator

The code is as follows Copy Code


Require ' 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 caching


$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


Require ' smarty.php ';


$tpl->assign (' title ', ' title Test ');


$tpl->assign (' content ', ' content test ');


$tpl->display (' template.html ');

The code is as follows Copy Code


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




</p><p><br/> <{$title}></p><p><br/>




<{$content}>



The code is as follows Copy Code




</p> <p><br/> <{$title}></p> & Lt;p><br/>




<{$content}>



http://www.bkjia.com/PHPjc/371918.html www.bkjia.com true http://www.bkjia.com/PHPjc/371918.html techarticle One, the annotations in the template each Smarty template file is developed using the syntax of the web foreground language (xhtml,css and JavaScript, etc.) combined with the Smarty engine. Use the web foreground to develop the language ...

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