PHP smarty Template Usage example detailed _php instance

Source: Internet
Author: User
Tags php script smarty template

This article has analyzed the Smarty template's usage method in detail, has certain reference value for the study smarty friend. Details are as follows:

comments in the template

Each smarty template file is developed by combining the syntax of the web foreground language (xhtml,css and JavaScript) with the Smarty engine.
The language used for the web foreground development is exactly the same as the original, and the annotations are unchanged.
The Smarty annotation syntax is the ' left terminator variable value * ' and ' * Right terminator variable value ', the contents of the two delimiters are commented content, can contain one or more lines, and the user browses the page to see the original code does not see the annotation, it is only the template internal annotation, the following is an example of the annotation.

$smarty->left_lelimiter = ' <{';
$smarty->right_delimiter = '}> ';

Comments:

<{* this a note *}>

Second, the template in the variable declaration

In Smarty, everything is dominated by variables, and all the rendering logic allows the template to control itself. There are several different types of variables, and the type of the variable depends on what symbol the prefix is or what symbol it is surrounded by. Variables can be directly output or parameters of the job function properties and modifiers, or for internal conditional expressions, and so on

<{$name}> <{* A generic type variable, you need to call the Assign function assignment value *}> <{
$contacts [row].phone}> <{* An array type variable within the template. You need to call the Assign function assignment value within the template *}>
<body bgcolor= "<{#bgcolor #}>" > <{* the value of the variable read from the configuration file and output *}>

If you output a variable from PHP in the Smarty template, you need to add the $ symbol in front of the variable and enclose it in the delimiter, named in the same way as the PHP variable, and the bounding notation is a bit like the <?php?> in PHP ( In fact, they're actually going to be replaced with this.

Third, the template output from the PHP variables allocated

There are two types of variables that are often used in the Smarty template: One is a variable that is assigned from PHP, and the other is a variable read from a configuration file
Note: The template can only output from the variables allocated in PHP, can not be in the template for these variables to be reassigned, variables are all domain, as long as the allocation of a one-time, if the allocation of more than two times, the variable content will be the final distribution of the main
The Foreach or section statement provided in Smarty is used to traverse each element of the output array, and the index array and 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 elements in an associative array are specified in the template by using '. ' Access to.
The way in which the object is accessed in the template, as in the PHP script, is done through the '-> ' operator.

mathematical calculation of variables in Smarty template

Variables cannot be directly assigned in a template, but can participate in mathematical operations, as long as the mathematical operations that can be performed in PHP scripts can be applied in the template, as follows:

<{$foo + 1}> <{* variable plus 1 *}>
<{$foo * $bar}> <{* Two variables multiplied *}> <{
$foo->bar-$bar [1] * $FO o->bar-3 * 7}> <{* Compound type variable participates in operation *}>
<{if ($foo + 2 = ten)}> <{* mathematical operations are applied in program logic *}>

A variable embedded in double quotes can be identified in the Smarty template, but some variables must be wrapped in inverted quotes ' (this symbol and ' ~ ' on the same key), as follows:

<{"Test $foo test"}> <{* double quotes using the variable *}>
<{"test" $foo [0] ' test '}> <{* the array variable enclosed in double quotes *}>
& Lt {"Test" $foo. Bar ' test '}> <{* the object member variable enclosed in double quotes *}>

Five,smarty template use examples :

1th Step: Load Smarty template engine, such as: Require ' libs/smarty.class.php '
2nd step: Create Smarty objects, such as: $smarty = new Smarty ();
3rd Step: Modify Smarty default behavior, such as: open cache, template storage path, etc.
Step 4th: The data obtained in the program is assigned to the corresponding variable in the template by the Assign () method of the Smarty object
Step 5th: 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, and it is easier to use
Prototype: void assign (String varname,mixed var)
This method can assign a type of data supported by PHP to a template variable that contains arrays and objects
There are two ways to use it:

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

Display () Method:

This method must be used in Smarty scripts and can only be used once in a script, which is responsible for obtaining and displaying templates referenced by the Smarty engine
Prototype: var display (string template[,string cache_id][,string compile_id])
Parameter one: template is required, specifies the type and path of a legitimate template resource
Parameter two: cache_id specifies the name of a cached identifier
Parameter three: COMPILE_ID is used when maintaining multiple caches on a page
Use the following methods
$smarty->display (' tpl/template_dir/template.html ');

Simple example:

1, Libs: Is Smarty class Library
2, Tpl/cache_dir: Storage cache Template
3, Tpl/compile_dir: Store the compiled template file
4, Tpl/config_dir: Storage of special configuration files
5, Tpl/template_dir: storage template file
6, smarty.php File New out of a Smarty class object, and set the property values of each object, the following code

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

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