First open the webpage http://smarty.php.net/download.php and download the latest smarty. Decompress the downloaded file (the directory structure is quite complex ). Next we will show you how to install an instance.
1. Installation
First open the webpage http://smarty.php.net/download.php and download the latest smarty. Decompress the downloaded file (the directory structure is quite complex ). Next we will show you how to install an instance.
(1) create a new directory named learn/under the root directory, and create a directory named smarty/In learn /. Copy the libs/from the extracted directory to smarty/, create the templates directory in smarty/, and create cache/, templates/, templates_c/, config/in templates /.
(2) create a new template file: index. tpl. Place the file in the learn/smarty/templates Directory. the code is as follows:
Create index. php and put this file under learn:
Require 'smarty/libs/smarty. class. php ';
$ Smarty = new Smarty; // set the path of each directory, which is the focus of installation
$ Smarty-> template_dir = "smarty/templates ";
$ Smarty-> compile_dir = "smarty/templates/templates_c ";
$ Smarty-> config_dir = "smarty/templates/config ";
$ Smarty-> cache_dir = "smarty/templates/cache ";
// The smarty template has the high-speed cache function. If this parameter is set to true, the caching is enabled, but the webpage will not be updated immediately. of course, you can use other methods to solve this problem.
$ Smarty-> caching = false;
$ Hello = "Hello World! "; // Assign a value
$ Smarty-> assign ("hello", $ hello); // reference the template file
$ Smarty-> display ('index. tpl ');?>
(3) execute index. php to see Hello World! .
II. Assignment
The values to be replaced in the template file are enclosed in braces {}, and $ is added before the value. For example, {$ hello }. This can be an array, for example, {$ hello. item1}, {$ hello. item2 }...
In the PHP source file, only a simple function assign (var, value) is required ).
Simple example:
*. Tpl:
Hello, {$ exp. name }! Good {$ exp. time}
*. Php:
$ Hello [name] = "Mr. Green ";
$ Hello [time] = "morning ";
$ Smarty-> assign ("exp", $ hello );
Output:
Hello, Mr. Green! Good morning
III. References
Generally, the headers and footer of webpages can be shared, so you only need to reference them in each tpl.
Example: *. tpl:
{Include file = "header. tpl "}
{* Body of template goes here *}
{Include file = "footer. tpl "}
IV. Judgment
In the template file, you can use judgment statements such as if else to place some logic programs in the template. "Eq", "ne", "neq", "gt", "lt", "lte", "le", "gte" "ge", "is even ", "is odd", "is not even", "is notodd", "not", "mod", "div by", "evenby", "odd ", "= ","! = ","> "," <"," <= ","> = "Are the comparisons that can be used in if. Let's see what it means.
Example:
{If $ name eq "Fred "}
WelcomeSir.
{Elseif $ name eq "Wilma "}
WelcomeMa 'Am.
Welcome, whatever you are.
{/If}
V. Loop
In Smarty, the method of traversing arrays cyclically is section. how to assign values to traversal is solved in the template, and only one assign in the php source file can solve the problem.
Example:
{* This examplewill print out all the values of the $ custid array *}
{Sectionname = customer loop = $ custid}
Id: {$ custid [customer]}
{/Section}
OUTPUT:
Id: 1000
Id: 1001
Id: 1002
6. FAQs
Smarty regards everything in braces {} as its own logic program, so we need literal help to insert javascript functions on the webpage, the literal function is to ignore braces {}.
Example:
Function isblank (field ){
If (field. value = '')
{Return false ;}
Else
{
Document. loginform. submit ();
Return true;
}
}
Script {/literal}
Smarty
{$ Hello}