Example of how to use variables in the PHP template engine Smarty, template smarty. Example of how to use variables in the PHP template engine Smarty. Template smarty this article describes how to use variables in the PHP template engine Smarty. For your reference, refer to the following example of how to use variables in the PHP template engine Smarty: Template smarty
This document describes how to use variables in the PHP template engine Smarty. We will share this with you for your reference. The details are as follows:
I. Overview:
Smarty is one of the many PHP template engines. it is a class library written according to PHP.
Advantages of Smarty:
1. optimize website access speed;
2. webpage front-end design and program separation;
II. install Smarty
1, need to go to the official site of Smarty http://www.smarty.net/download.php to download the latest Smarty version, such as download version: Smarty-2.6.18.tar.tar;
2, unzip the Smarty-2.6.18.tar.tar compressed package, will find a lot of files and folders, except the libs folder, all other delete, are useless;
3. when the Smarty template engine is called, use the PHP require statement to load the libs/Smarty. class. php file.
III. default settings of the Smarty class library
Require into Smarty. class. after the php file, if you need to set members in the Smarty class library, there are two methods: one is directly in the Smarty. class. modify in the php file. one is to re-specify the class library after initialization. The latter is generally used. The following describes the member attributes in the Smarty class library:
1. $ template_dir: Set the directory where Template files are stored on the website. The default directory is templates.
2. $ compile_dir: Set the directory for storing compiled files on the website. The default directory is templates_c.
3. $ config_dir: defines the directory used to store the special configuration files of the template. the default value is configs.
4. $ left_delimiter: used for the left Terminator variable in the template. the default value is '{'
5. $ right_delimiter: used for the right Terminator variable in the template. the default value is '}'
IV. use of variables:
All the accesses to Smarty are variable-based. The following describes how to use an instance.
Example: the main file introduces the template initialization configuration file (init. inc. php) and a class, and assigns values to the variables in the template.
First, set the init. inc. php file as the initialization configuration file of the Smarty template.
Init. inc. php
<? Php define ('root _ path', dirname (_ FILE _); // defines the website ROOT directory require ROOT_PATH. '/libs/Smarty. class. php '; // load the Smarty file $ _ tpl = new Smarty (); // instantiate an object $ _ tpl-> template_dir = ROOT_PATH. '/tpl/'; // reset the Template directory to the tpl directory $ _ tpl-> compile_dir = ROOT_PATH under the root directory. '. /com/'; // reset the compilation directory to the com directory $ _ tpl-> left_delimiter =' <{'; // reset the left delimiter to '<{' $ _ tpl-> right_delimiter = '}>'; // reset the left delimiter to '}>'?>
Index. php of the main file
<? Php require 'init. inc. php'; // introduce the template initialization file require 'persion. class. php '; // load the object file global $ _ tpl; $ title = 'This is a title! '; $ Content = 'This is body content! ';/** 1. allocate Template variables from PHP; * dynamic data (variables generated by PHP from databases or files and algorithms) * any type of data can be allocated from PHP, mainly including * scalar: string, int, double, boolean * combination: array and object * NULL * index arrays are * associated arrays directly accessed through indexes, rather than [correlated subscripts. the following method is used: * the object is directly accessed through-> **/$ _ tpl-> assign ('title', $ title ); $ _ tpl-> assign ('content', $ content); // variable assignment $ _ tpl-> assign ('arr1 ', array ('ABC ', 'def ', 'ghi'); // The value of the index array $ _ tpl-> assign ('arr2', array ('ABC', 'Def ', 'ghi'), array ('J Kl ', 'mno', 'pqr'); // The value of the two-dimensional index array $ _ tpl-> assign ('arr3 ', array ('one' => '123', 'two' => '123', 'Three '=> '123 ')); // assign a value to the associated array $ _ tpl-> assign ('arr4 ', array ('one' => '123 ', 'two' => '123'), 'two' => array ('Three '=> '123', 'Four' => '123 '))); // assign a value to the correlated two-dimensional array $ _ tpl-> assign ('arr5 ', array ('one' => array ('123456', '123456 '), array ('Three '=> '20140901', '20160901'); // assign values to join and index Hybrid Arrays $ _ tpl-> assign ('object ', new Persion ('xiaoyi', 10); // object value assignment // Smarty You can also perform operations on values (+-*/^ ......) $ _ Tpl-> assign ('num1', 10); $ _ tpl-> assign ('num2', 20); $ _ tpl-> display ('index. tpl ');?>
Index. tpl, template file of index. php in the main file (put in the/tpl/directory)
<{$ Title}> Variable access: <{$ content}>
Access to the index array: <{$ arr1 [0] }>< {$ arr1 [1] }>< {$ arr1 [2]}>
Access to an indexed two-dimensional array: <{$ arr2 [0] [0] }>< {$ arr2 [0] [1] }>< {$ arr2 [0] [2] }>< {$ arr2 [1] [0] }>< {$ arr2 [1] [1] }>< {$ arr2 [1] [2]}>
Access to the associated array: <{$ arr3.one }><{$ arr3.two }><{$ arr3.three}>
Access to a two-dimensional array: <{$ arr4.one. one}> <{$ arr4.one. two}> <{$ arr4.two. three}> <{$ arr4.two. four}>
Access to join and index Hybrid Arrays: <{$ arr5.one [0] }>< {$ arr5.one [1] >>< {$ arr5 [0]. three }> <{$ arr5 [0] [0]}>
Access to member variables in the object: <{$ object-> name }>< {$ object-> age}>
Access to methods in the object: <{$ object-> hello ()}>
Variable calculation: <{$ num1 + $ num2}>
Mixed variable operation: <{$ num1 + $ num2 * $ num2/$ num1 + 44}>
Persion. class. php
<? Php class Persion {public $ name; // Set to public $ age for convenient access; // define a constructor public function _ construct ($ name, $ age) {$ this-> name = $ name; $ this-> age = $ age;} // defines a hello () method, and outputs the name and age public function hello () {return 'Hello! My name is '. $ this-> name.'. $ this-> age. ';}}?>
Execution result:
Variable access: This is body content! Access to the index array: access to the abc def ghi index two-dimensional array: access to the abc def ghi jkl mno pqr associated array: 111 222 333 access to the associated two-dimensional array: 111 222 333 444 join and index Hybrid array access: 111 222 333 444 access to member variables in an object: access to methods in a small and easy 10 object: Hello! My name is Xiaoyi. I am 10 years old. Variable operation: 30 variable mixed operation: 94