Smarty tutorial (basic syntax)

Source: Internet
Author: User
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}> <{/Section}>

<{$ Stu [s]}>


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.


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.