(Correct version) Quick Start tutorial for smarty-PHP Tutorial

Source: Internet
Author: User
(Correct version) smarty Quick Start Guide for half an hour. I. smarty program design part: in the template design section of smarty, I briefly introduced some common settings of smarty in the template, this section describes how to design a smarty program:

In the template design section of smarty, I briefly introduced some common settings of smarty in the template. this section mainly introduces How to Start program design in smarty. Download the Smarty file and place it on your site.
Index. php PHP code:
/**
*
* @ Version $ Id: index. php
* @ Package
* @ Author www.2cto.com
* @ Action: Display the instance program
*/
Include_once ("./Smarty. class. php"); // contains the smarty class file

$ Smarty = new Smarty (); // Create a smarty instance object $ smarty
$ Smarty-> templates ("./templates"); // you can specify the Template directory.
$ Smarty-> templates_c ("./templates_c"); // you can specify the compiling directory.
$ Smarty-> cache ("./cache"); // cache Directory
$ Smarty-> cache_lifetime = 0; // cache time
$ Smarty-> caching = true; // cache method

$ Smarty-> left_delimiter = "{#";
$ Smarty-> right_delimiter = "#}";
$ Smarty-> assign ("name", "zaocha"); // replace Template variables
$ Smarty-> display ("index.htm"); // compile and display the index.htm template under./templates
?>

II. explain the smarty program

As we can see, the smarty program is actually a set of code that complies with the php language specification. let's explain it one by one:
1:/**/statement:
The included part is the program header annotation. The main content should be a brief introduction to the role of the program, copyright and the author and the writing time, which is not necessary in the smarty, but in terms of the program style, this is a good style.

2: include_once statement:
It includes the smarty file installed on the website to the current file. Note that the included path must be correctly written.

3: $ smarty = new Smarty ():
This statement creates a Smarty object $ smarty, which is a simple object instantiation.

4: $ smarty-> templates (""):
This statement specifies the path for the $ smarty object to use the tpl template. it is a directory. if this statement is not provided, the default template path of Smarty is the templates Directory of the current directory, when writing a program, we need to write this statement, which is also a good program style.

.

5: $ smarty-> templates_c (""):
This statement specifies the directory when the $ smarty object is compiled. In the template design article, we already know that Smarty is a compilation template language, and this directory is the directory for compiling the template. Note that if the site is on a linux server, make sure that

This directory defined in teamplates_c has writable and readable permissions. by default, its compiling directory is templates_c under the current directory. for the same reason, we will write it clearly.

6: $ smarty-> left_delimiter and $ smarty-> right_delimiter:
Specifies the left and right delimiters when searching for template variables. The default values are "{" and "}", but actually, because we want to use "script" in the template, the function definition in the Script will inevitably use {}, although it has its own solution, we are used to redefining it.

For "{#" and "#}" or" "Or other flags. note: If the left and right delimiters are defined here, each variable must use the same symbol as the definition in the template file, for example, if you specify "<{" and "}>" here, the htm template also needs

Change {$ name} to <{$ name}> so that the program can find the template variables correctly.
7: $ smarty-> cache ("./cache "):
Tell the template file cache location output by Smarty. In the previous article, we learned that the biggest advantage of Smarty is that it can be cached. here, we set the cache directory. By default, it is the cache directory under the current directory, which is equivalent to the templates_c directory.

Make sure it is readable and writable.

8: $ smarty-> cache_lifetime = 60*60*24:

Here, the effective cache time is calculated in seconds. When the first cache time expires, when the caching variable of Smarty is set to true, the cache will be rebuilt. When the value is-1, it indicates that the created cache never expires. if it is 0, it indicates that the cache is slow every time the program is executed.

The storage is always re-created. The preceding settings indicate that cache_lifetime is set to one day.

9: $ smarty-> caching = 1:
This attribute tells Smarty whether to cache and how to cache it. It can take three values, 0: Smarty default value, indicating that the template is not cached; 1: indicates that Smarty will use the current defined cache_lifetime to determine whether to end the cache; 2: Indicates

Smarty uses the cache_lifetime value when the cache is created. Traditionally, true and false are used to indicate whether the cache is performed.

10: $ smarty-> assign ("name", "zaocha "):
The prototype of this number is assign (string varname, mixed var), varname is the template variable used in the template, and var indicates the variable name to replace the template variable; the second prototype is assign (mixed var). We will explain in detail how to use this member function in the following example. assign is one of the core functions of Smarty, all replace Template variables must use it.

11. $ smarty-> display ("index. tpl "):
Display (string varname) is used to display a template. To put it simply, it displays the templates that have been analyzed and processed. the template file here does not need to be added with a path. you only need to use a file name, the path is defined in $ smarty-> templates (string path.
After the program is executed, we can open the templates_c and cache directories under the current directory, and we will find that there are more % directories below, which are the compilation and cache directories of Smarty, it is automatically generated by the program. do not directly modify these generated files.
I briefly introduced some common basic elements in the Smarty program. in the following example, you can see that they will be used multiple times.

III. Template description

Next we will introduce a section loop block and a foreach loop block. Originally, it should belong to the template part, but because they are the essence of smarty and closely related to the smarty program design part, so let's talk about it separately in this section.

1: foreach: Used to loop a simple array. it is a selective section loop. Its Definition format is:

{Foreach from = $ array item = array_id}

{Foreachelse}

{/Foreach}

Here, from indicates the array variable to be cyclic. item is the name of the variable to be cyclic, and the number of loops is determined by the number of array variables specified by from. {Foreachelse} is used for processing when the passed array in the program is empty. The following is a simple example:

Template File: example.htm

  

  

  

Foreach outputs a "two-dimensional join array" data:

{# Foreach item = new from = $ news #}

News No.: {#$ new. id #}

News content: {#$ new. title #}


--------------------------------------------------------------------------------

{# Foreachelse #}

No news output in the database!

{#/Foreach #}

  


--------------------------------------------------------------------------------

  

  

Note: bkJia above the writing is like this :( 2cto.com link: http://www.BkJia.com/html/webkaifa/PHP/PHPyingyong/2009/0429/2897.html)

{Foreach from = $ newsArray item = newsID}

News No.: {$ newsID}

News: {$ newsTitle}


--------------------------------------------------------------------------------

{Foreachelse}

Sorry, there is no news output in the database!

{/Foreach}

This is an error that does not display data. This article has corrected it. Run properly, as shown in the following figure:

  


In the template design section of smarty, I briefly introduced some common settings of smarty in the template. this section mainly introduces how...

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.