Getting started with Smarty Templates

Source: Internet
Author: User
Smarty Templates are a popular template. in the php development process, many developers regard them as one of the most friendly templates.

The smarty template is a popular template. many developers regard it as one of the most friendly templates during the php development process. learning the smarty course is also included in one of the training courses for many training institutions, so we need to learn a lot.

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 ). For example:
(1) I created a new directory named learn/under the root directory, and then created a folder 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 /,
(2) create a new template file: index. tpl. Place the file in the learn/smarty/templates Directory. the code is as follows:




Smarty


{$ Hello}


Create index. php and put this file under learn:
// Reference a class file
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.

{Else}
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 *}
{Secti 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's help to insert the javascr into pt function on the webpage, the literal function is to ignore braces {}.
Example:
{Literal}

Function isblank (field ){
If (field. value = '')
{Return false ;}
Else
{
Document. loginform. submit ();
Return true;
}
}

{/Literal}

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.