Smarty Templates Quick Start _php templates

Source: Internet
Author: User
Tags smarty template
A wide variety of template classes have emerged in the PHP world, but Smarty has always been a leader in terms of functionality and speed, because Smarty is relatively powerful, so it is slightly more complex to use than some other template classes. Take 30 minutes to get started quickly.

I. Installation

First open the Web page http://smarty.php.net/download.php, download the latest version of the Smarty. Unzip the downloaded file (the directory structure is quite complex). Next I will show you a installation example, see should be extrapolate.
(1) I created a new directory learn/in the root directory, and then set up a directory smarty/in learn/. The libs/copy of the directory that you just unzipped into the smarty/, and then in smarty/new Templates directory, templates in new cache/,templates/,templates_c/, config/.

(2) Create a new template file: Index.tpl, put this file in the Learn/smarty/templates/templates directory, the code is as follows:
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<title>Smarty</title>
<body>
{$hello}
</body>




To create a new index.php, place this file under learn/:
Copy Code code as follows:
<?php
Referencing class files
Require ' smarty/libs/smarty.class.php ';

$smarty = new Smarty;

Set the path of each directory, this 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, open caching, but will cause the Web page is not immediately updated problem, of course, can also be resolved by other means
$smarty->caching = false;

$hello = "Hello world!";
assigning values
$smarty->assign ("Hello", $hello);

Referencing template files
$smarty->display (' Index.tpl ');

?>




(3) The executive index.php will be able to see Hello world!.

Two. Assigning value

The values that need to be replaced in the template file are enclosed in braces {}, preceded by the $ 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. Reference
Web pages in a Web site general headers and footer are common, so just refer to them in each TPL.
Example: *.tpl:
{include file= "HEADER.TPL"}

{* Body of template goes-here *}

{include file= "FOOTER.TPL"}

Four. Judge
In the template file, you can use the if else and other judgment statements, that is, you can put some logic programs in the template. "eq", "ne", "neq", "GT", "LT", "LTE", "Le", "GTE" "GE", "is even", "is odd", "are not even", "are not odd", "not", "mod", "Div by", "even by", "Odd by", "= =", "!=", ">", "<", "<=", ">=" These are the comparisons that can be used in the IF. You can see what you mean.

Example:
{if $name eq "Fred"}

Welcome Sir.

{elseif $name eq "Wilma"}

Welcome Ma ' am.


{Else}
Welcome, whatever you are.

{/if}


Five. Cycle

The method used to iterate through an array in Smarty is the section, how the assignment traversal is solved in the template, as long as a assign in the PHP source file can solve the problem.
Example:
{* This example'll print out all of the values of the $custid array *}

{section Name=customer loop= $custid}

ID: {$custid [customer]}<br>
{/section}

OUTPUT:

Id:1000<br>
Id:1001<br>
Id:1002<br>

Six. Frequently Asked Questions

Smarty all braces {} as their own logic program, so we want to insert JavaScript functions in the Web page will need literal help, literal function is to ignore the curly braces {}.
Example:
{literal}
<script language=javascript>

function Isblank (field) {

if (Field.value = = ")
{return false;}

Else
{

Document.loginform.submit ();
return true;

}

}

</script>
{/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.