Smarty tutorial (basic syntax) 1. smarty configuration
First, the first thing to do with smarty is to configure it first. there are usually the following nine lines of code:
Require_once ("smarty/libs/Smarty_class.php"); // include the smarty class definition file
$ Smarty = new smarty ();
$ Smarty-> config_dir = "smarty/libs/Config_File.class.php ";
$ Smarty-> caching = false; // whether to use the cache. during project debugging, it is not recommended to enable the cache.
$ Smarty-> cache_dir = "smarty_cache/"; // cache folder
$ Smarty-> template_dir = "smarty_tpl"; // template folder
$ Smarty-> compile_dir = "smarty_compile"; // compile the folder
$ Smarty-> left_delimiter = "<{"; // tag operator definition is not necessary. smarty uses "<" and ">" by default. it is strongly recommended that you change it.
// If The smarty tag is in a javascript statement, there is a high possibility of conflict.
$ Smarty-> right_delimiter = "}> ";
The preceding nine lines of code can be stored in an independent file, which must be referenced on the smarty page.
2. use of smarty
Syntax for replacing tags with smarty:
Smarty-> assign ("tag name", "value ");
Smarty-> display ("index.html"); // display Content. index indicates the template file name.
Assume that the template file contains a tag <{$ user_name}> (note: the variable in the tag must contain $)
In the php file, you can:
$ New_name = "Joan ";
Smarty-> assign ("user_name", $ new_name); (note: At this time, user_name does not contain $)
Smarty-> display ("index.html"); // display Content. index indicates the template file name.
3. smarty cycle
Loop Processing in php files is like processing common labels.
Template File Code:
<{Section name = s loop = $ stu}>
<{$ Stu [s]}>
|
<{/Section}>
In the php file, you can:
Assume there is an array or a record set $ rs
$ Smarty-> assign ("stu", $ rs );
$ Smarty-> display ("index.html"); // process the cyclic tag in the PHP file and the common tag.
Original tutorial: www.37dg.com (Wang Songyuan)
Please indicate the source for reprinting. thank you for your cooperation.
4. if syntax of smarty
Template File
<{If $ my_r> 500}>
<{$ My_r}>
<{/If}>
Php file:
$ Aa = 123;
$ Smarty-> assign ("my_r", $ aa );
$ Smarty-> display ("in.html ");
In the preceding example, if $ aa> 500, the my_r tag is displayed, otherwise it is not displayed.
<{If condition}> // Do not enclose the condition or add then in the background like the basic language.
<{/If}>
5. smarty cyclically works with if to use instances
Php file
----------------------------------
$ Aa [0] = 123;
$ Aa [1] = 456;
$ Aa [2] = 789;
$ Smarty-> assign ("my_r", $ aa );
$ Smarty-> display ("in.html ");
Template File
------------------------------------------------------
<{Section name = s loop = $ my_r}>
<{If $ my_r [s]> 200}>
<{$ My_r [s]}>
<{Else}>
200 or less
<{/If}>
<{/Section}>
-----------------------------------------------
In the preceding example, only values greater than 200 are displayed.