PhpSmarty template usage _ PHP Tutorial

Source: Internet
Author: User
The usage of the phpSmarty template is described in detail. I. Note that every Smarty template file in the template is developed using the Web front-end Language (xhtml, css, and javascript) combined with the syntax of the Smarty engine. Used web front-end development language 1. annotations in the template


Every Smarty template file is developed by combining the Web front-end Language (xhtml, css, and javascript) with the syntax of the Smarty engine.


The web front-end development language used is exactly the same as the original one, and the annotations have not changed.


The Smarty annotation syntax is 'left Terminator variable value' and '* right Terminator variable value'. the content between the two delimiters is the annotation content, which can contain one or more lines, the comments are not displayed when you view the original code on the webpage. they are only comments in the template. Below is a small example of comments.


$ Smarty-> left_lelimiter = '<{';

$ Smarty-> right_delimiter = '}> ';

Note: <{* this a note *}>

The code is as follows:

$ Smarty-> left_lelimiter = '<{';

$ Smarty-> right_delimiter = '}> ';

Note: <{* this a note *}>

II. variable declaration in the template


In Smarty, all variables are dominated, and all presentation logic is controlled by the template.


There are several different types of variables. the type of a variable depends on the symbols whose prefix is or are surrounded by symbols.


Variables can be directly output, job function attributes, modifier parameters, or internal conditional expressions.


<{$ Name }>< {* regular type variable, which must call the assign function allocation value in the template *}>


<{$ Contacts [row]. phone}> <{* array type variable. you need to call the assign function allocation value in the template *}>


<{* Value of the variable read from the configuration file and output *}>


If you output the variables allocated from php in the Smarty template, you need to add a $ symbol before the variable and enclose it with a separator. the naming method is the same as that of php variables, in addition, the delimiter is a bit like the one in php. (In fact they will be replaced with this)


3. output the variables allocated from php in the template


There are two variables frequently used in the Smarty Template: one is the variables allocated from php, and the other is the variables read from the configuration file.


Note: only the variables allocated from php can be output in the template. you cannot assign values to these variables in the template. all the variables are global. you only need to assign them once, if more than two times are allocated, the variable content is based on the last allocation.


The foreach or section statement provided by Smarty is used to traverse each element in the output array. the output method of the index array and associated array in the template is slightly different, the access method of the index array in the template is the same as the reference in the php script. the method specified by the element in the associated array in the template is '. 'accessed.

The method for accessing objects in the template is the same as that in the php script. the '->' operator is used.


IV. mathematical calculation of variables in the Smarty Template

Variables in the template cannot be directly assigned values, but can be involved in mathematical operations. any mathematical operations that can be executed in the php script can be applied in the template, as shown below:

<{$ Foo + 1 }>< {* Variable Plus 1 *}>


<{$ Foo * $ bar}> <{* multiply two variables *}>


<{$ Foo-> bar-$ bar [1] * $ foo-> bar-3*7}> <{* compound type variable involved in calculation *}>


<{If ($ foo + 2 = 10)}> <{* application of mathematical operations in program logic *}>

The code is as follows:

<{$ Foo + 1 }>< {* Variable Plus 1 *}>


<{$ Foo * $ bar}> <{* multiply two variables *}>


<{$ Foo-> bar-$ bar [1] * $ foo-> bar-3*7}> <{* compound type variable involved in calculation *}>


<{If ($ foo + 2 = 10)}> <{* application of mathematical operations in program logic *}>


Variables embedded in double quotation marks can be recognized in the Smarty template, but some variables must be enclosed with backquotes ''' (this symbol and '~ 'Wrap on the same key, as shown below:

The code is as follows:


<{"Test $ foo test" }>< {* variable used in double quotation marks *}>


<{"Test' $ foo [0] 'test"}> <{* array variable enclosed by reverse quotation marks in double quotation marks *}>


<{"Test' $ foo. bar 'test"}> <{* object member variable enclosed in double quotation marks *}>


<{"Test $ foo test" }>< {* variable used in double quotation marks *}>


<{"Test' $ foo [0] 'test"}> <{* array variable enclosed by reverse quotation marks in double quotation marks *}>


<{"Test' $ foo. bar 'test"}> <{* object member variable enclosed in double quotation marks *}>

Step 2: load the Smarty template engine, for example, require 'libs/Smarty. class. php'


Step 2: Create a Smarty object, for example, $ smarty = new Smarty ();


Step 2: modify the default behavior of Smarty, such as enabling cache and storing the template.


Step 1: assign the data obtained in the program to the corresponding variable in the template using the assign () method of the Smarty object


Step 2: 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 values of types supported by php to template variables including arrays and objects.


You can use either of the following methods:

// Specify a pair of 'name/number'
$ Smarty-> assign ('question', '');
$ Smarty-> assign ('answer', 'not very good ');


// Specify the value that contains 'name/number'
$ Smarty-> assign (array ('question' => '', 'answer' =>' '); // This method is rarely used.
Display () method


This method must be used in a Smarty-based script and can only be used once. it is used to obtain and display templates referenced by the Smarty engine.


Prototype: var display (string template [, string cache_id] [, string compile_id])


Parameter 1: template is required. it specifies the type and path of a valid template resource.


Parameter 2: cache_id specifies the name of a cache identifier


Parameter 3: compile_id is used to maintain multiple caches on a page.


The usage is as follows:
$ Smarty-> display ('tpl/template_dir/template.html ');


Simple instance

1. libs: Smarty class library


2. tpl/cache_dir: stores cache templates.


3. tpl/compile_dir: stores the compiled template file.

4. tpl/config_dir: Stores special configuration files

5. tpl/template_dir: stores template files.

6. in the smarty. php file, a new Smarty class object is generated and the attribute values of each object are set. the following code:


Require 'libs/Smarty. class. php'; // load the Smarty. class. php file


Define ('site _ root', './tpl/'); // defines a constant

$ Tpl = new Smarty ();


$ Tpl-> template_dir = SITE_ROOT. 'Template _ dir'; // Save the template file


$ Tpl-> compile_dir = SITE_ROOT. 'compile _ dir'; // Save the compiled template file.


$ Tpl-> config_dir = SITE_ROOT. 'config _ dir'; // Save the special configuration file


$ Tpl-> cache_dir = SITE_ROOT. 'cache _ dir'; // Save the Smarty cache file


$ Tpl-> caching = 1; // enable cache


$ Tpl-> cache_lifetime = 60*60*24; // cache time 1 day


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


$ Tpl-> right_delimiter = '}>'; // right end character

The code is as follows:


Require 'libs/Smarty. class. php'; // load the Smarty. class. php file


Define ('site _ root', './tpl/'); // defines a constant

$ Tpl = new Smarty ();


$ Tpl-> template_dir = SITE_ROOT. 'Template _ dir'; // Save the template file


$ Tpl-> compile_dir = SITE_ROOT. 'compile _ dir'; // Save the compiled template file.


$ Tpl-> config_dir = SITE_ROOT. 'config _ dir'; // Save the special configuration file


$ Tpl-> cache_dir = SITE_ROOT. 'cache _ dir'; // Save the Smarty cache file


$ Tpl-> caching = 1; // enable cache


$ Tpl-> cache_lifetime = 60*60*24; // cache time 1 day


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


$ Tpl-> right_delimiter = '}>'; // right end character

7. the index. php file homepage code is as follows:


Require 'smarty. php ';


$ Tpl-> assign ('title', 'title ');


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


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

The code is as follows:


Require 'smarty. php ';


$ Tpl-> assign ('title', 'title ');


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


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

8. tpl/template_dir/template.html. the code for this template file is as follows:




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




<{$ Content}>



The code is as follows:




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




<{$ Content}>



Every Smarty template file is developed using the Web front-end Language (xhtml, css, and javascript) combined with the syntax of the Smarty engine. The web front-end development language used...

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.