The usage of Smarty-related considerations and several ways to share access variables, you can refer to the need of friends. $ Tpl = new Smarty (); // create a new smarty object. what I use is
Smarty-1, 3.1.6Version
1. set the path to the smarty template $ tpl->
SetTemplateDir ()By default
Templates
2. set the path for compiling the smarty template $ tpl->
SetCompileDir ()By default
Templates_c
3. set the left and right separators of The smarty template engine,
$ Tpl-> left_delimiter = "<{";
$ Tpl-> right_delimiter = "}> ";
By default: public $ left_delimiter = "{"; // smarty source code
Public $ right_delimiter = "}"; // smarty source code
Why should we change these delimiters?
For example, in an earlier version of the smarty engine template, an error is reported and cannot be identified automatically.
For example:
Or javascript
The code is as follows:
Script
Function show (){
Alert ("smarty ");
}
Script
In both cases, there are "left and right braces", and an error will be reported when the smarty engine encounters
4. for initialization, we can create another php file for initialization, such as smarty. ini. php. Then include it in the PHP file.
The code is as follows:
Include "../Smarty3.1.6/libs/Smarty. class. php ";
$ Tpl = new Smarty ();
$ Tpl-> setTemplateDir ("./Tpl ");
$ Tpl-> setTemplateDir ("./Compile ");
$ Tpl-> left_delimiter = "<{";
$ Tpl-> right_delimiter = "}> ";
?>
5. when using the display function or include other templates of the smarty template engine, the template directory specified in the smarty object (such as the Tpl directory, which is the default templates directory) is used as the base directory.
① The Template directory is Tpl, which stores many templates, including default, green, red, and default templates. There are many template files (index. tpl, header. tpl, footer. tpl), the correct usage of display at this time: $ tpl-> display ("default/index. tpl); that is, the default template directory under the Base Directory
② In the template file (such as index. tpl) contains other template files (such as header. tpl, footer. tpl), the correct format of include should be: <{include "default/header. tpl "}>,< {include" default/footer. tpl "}>
Although index. tpl, header. tpl, footer. tpl is in the same directory, but <{include "header. tpl "}>, <{include" footer. tpl "}> is incorrect syntax. in this case, the smarty engine will find the header and footer in the Tpl Directory, instead of the default directory.
6. if you want PHP programs under various directories to load Smarty, use the template directory specified by Smarty, and compile the directory, the only way is to use the absolute path.
7. how to access variables in the Smarty template engine (remember to add the "$" symbol before the variables in the template)
① Access an array
Index array:
$ Tpl-> assign ("arr", array ("aa", "bb", "cc "));
$ Tpl-> assign ("arr2", array ("two-dimensional arrays one by one", "two-dimensional arrays one or two"), array ("two-dimensional arrays one by one ", "Two-dimensional array 2 ")));
Access Index array: <{$ arr [0]}>, <{$ arr [0]}>, <{$ arr [0]}>
Access the two-dimensional index array: <{$ arr2 [0] [0] }>,< {$ arr2 [0] [1]}>
Join array: (use the. symbol to access)
Access associated array: <{$ arr3.id} >,< {$ arr3.name} >,< {$ arr3.age}>
② Access object
Creation object:
The code is as follows:
Class human {
Private $ sex;
Private $ name;
Private $ age;
Public function _ construct ($ s, $ n, $ ){
$ This-> sex = $ s;
$ This-> name = $ n;
$ This-> age = $;
}
Public function print_info (){
Return $ this-> sex. "--". $ this-> name. "--". $ this-> age;
}
}
$ Tpl-> assign ("student", new human ("male", "MarcoFly", 22 ));
Assign a value to the object in the Template: <{$ student-> print_info ()}>
8. mathematical operations in the Smarty template engine can be applied to template variables.
Assign values to variables
$ Tpl-> assign ("num1", 10 );
$ Tpl-> assign ("num2", 5.5 );
Template variable output
<{$ Num1}> // result 10
<{$ Num2}> // result 5.5
<{$ Num1 + $ num2}> // result 15.5
<{$ Num1 + $ num2 * $ num2/$ num1}> // result 13.025
Original article
Reprinted please note: WEB Development _ Xiaofei