Smarty template quick start _ php template

Source: Internet
Author: User
Tags php template
The quick start of Smarty Templates has already seen a variety of template classes in the PHP world, but Smarty is still in the leading position in terms of functionality and speed, because Smarty is relatively powerful, therefore, it is a little more complex to use than some other template classes. Now you can get started in 30 minutes.

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, I will demonstrate how to install an instance.
(1) I created a new directory learn/under the root directory, and then created 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:

The code is as follows:






Smarty


{$ Hello}





Create index. php and put this file under learn:

The code is as follows:

// 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 not odd", "not", "mod", "p by", "even by", "odd ", "= ","! = ","> "," <"," <= ","> = "Are the comparisons that can be used in if. Let's see what it means.

Example:
{If $ name eq "Fred "}

Welcome Sir.

{Elseif $ name eq "Wilma "}

Welcome m'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 example will print out all the values of the $ custid array *}

{Section name = 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:
{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.