Smarty Template engine Getting Started tutorial

Source: Internet
Author: User
Tags smarty template

Smarty template is a more popular template, in the PHP development process by many developers as one of the most friendly template, learning Smarty course for many training institutions is also included in one of the training courses, so many aspects of the need for us to learn
One. Installation
First open the Web page http://smarty.php.net/download.php, download the latest version of Smarty. Unzip the downloaded file (the directory structure is quite complex). For example:
(1) I set up a new directory in the root directory learn/, and then set up a folder smarty/in learn/. Copy the libs/of the directory you just extracted into the smarty/, and then create a new templates directory in smarty/, templates, cache/,templates/,templates_c/,
(2) Create a new template file: Index.tpl, put this file in the Learn/smarty/templates/templates directory, the code is as follows:
<! DOCTYPE HTML Public "-//w3c//dtdhtml 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<metahttp-equiv= "Content-type" c>
<title>Smarty</title>
<body>
{$hello}
</body>
Create a new index.php and place this file under learn/:
<?php
Referencing a class file
Require ' smarty/libs/smarty.class.php ';
$smarty = new Smarty;
Set the path of each directory, here is the focus of the installation
$smarty->template_dir = "Smarty/templates/templates";
$smarty->compile_dir = "Smarty/templates/templates_c";
$smarty->config_dir = "Smarty/templates/config";
$smarty->cache_dir = "Smarty/templates/cache";
Smarty template has the function of caching, if this is true then open caching, but will cause the page not immediately update the problem, of course, can also be resolved by other means
$smarty->caching = false;
$hello = "Hello world!";
Assign value
$smarty->assign ("Hello", $hello);
Referencing template files
$smarty->display (' Index.tpl ');
?>
(3) Execute index.php to see Hello world!.
Two. Assigning values
The value that needs to be replaced in the template file is enclosed in curly braces {}, and the value is preceded by a $ number. For example {$hello}. This can be an array, such as {$hello. item1},{$hello. item2} ...
The PHP source file only needs a simple function assign (var, value).
A 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
Three. References
Web pages in a Web site the header and footer are common, so you can refer to them in each TPL.
Example: *.tpl:
{include file= "HEADER.TPL"}
{* Body of template goes here *}
{include file= "FOOTER.TPL"}
Four. Judging
In a template file, you can use the If Else judgment statement, which allows you to put some logic programs in the template. "eq", "ne", "neq", "GT", "LT", "LTE", "Le", "GTE" "GE", "is even", "was odd", "is not even", "was notodd", "not", "mod", "Div b Y "," Evenby "," Odd by "," = = ","! = "," > "," < "," <= "," >= "These are the comparisons that can be used in the IF. Just look at it and you'll know what it means.
Example:
{if $name eq "Fred"}
Welcomesir.
{elseif $name eq "Wilma"}
Welcomema ' AM.
{Else}
Welcome,whatever is.
{/if}
Five. Cycle
In Smarty, the method of iterating through an array is section, how the assignment traversal is resolved in the template, and the PHP source file can solve the problem with just one assign.
Example:
{* This examplewill print out all the values of the $custid array *}
{Secti loop= $custid}
ID: {$custid [customer]}<br>
{/section}
OUTPUT:
Id:1000<br>
Id:1001<br>
Id:1002<br>
Six. Frequently Asked Questions
Smarty all the braces {} are considered to be their own logic program, so we want to insert the JAVASCRĪPT function in the Web page need literal help, literal function is to ignore the curly braces {}.
Example:
{literal}
<scrīptlanguage=javascrīpt>
function Isblank (field) {
if (Field.value = = ")
{return false;}
Else
{
Document.loginform.submit ();
return true;
}
}
</scrīpt>
{/literal}

Smarty Template engine Getting Started tutorial

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.