How to use PHP Smarty template

Source: Internet
Author: User
Tags php file php script require smarty template

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 a small example of the annotation.


The code is as follows Copy Code

$smarty->left_lelimiter = ' <{';

$smarty->right_delimiter = '}> ';

Comment: <{* this a note *}>

Second: The variable declaration in the template


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 it is prefixed to 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 within the template *}>


<{$contacts [row].phone}> <{* Array type variable, 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.


Three: Output variables from PHP in the template


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.


Four: Mathematical calculation of the variables in the 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:

The code is as follows Copy Code

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


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


<{$foo->bar-$bar [1] * $foo->bar-3 * 7}> <{* Compound type variable participates in the Operation *}>


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

The code is as follows Copy Code


<{"Test $foo test"}> <{* double quotes using variables *}>


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


<{"Test" $foo. Bar ' test '}> <{* the object member variable enclosed in double quotes *}>


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

The code is as follows Copy Code

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


Specify include ' name/value '
$smarty->assign (' question ' => ' hello ', ' answer ' => ' not very good '));//This way is less


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

The code is as follows Copy Code

<?php


Require ' libs/smarty.class.php ';//loading 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 End character


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

7, index.php file home code as follows

  code is as follows copy code

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

The code is as follows Copy Code



<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>


<title>


<{$title}>


</title>



<body>


<{$content}>


</body>


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.