Quick getting started with smarty

Source: Internet
Author: User
Smarty's program design section: in the template design section of smarty, I briefly introduced some common settings of smarty in the template, this section describes how to start program design in smarty. PHP code :----------------------------------------------------------------

The program design section of smarty:

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 our process in smarty.

Sequential design.

PHP code :--------------------------------------------------------------------------------

First, we will introduce some elements in the. php file we used in the previous section. Similarly, let's take the index. php file at the beginning of the previous section as an example:

========================================================== ==========
Index. php
========================================================== ==========
/*************************************** ******
*
* File name: index. php
* For use: Display instance programs
*
**************************************** *****/
Include_once ("./comm/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.


// *** NOTE: I am a new *** here ****//
$ Smarty-> cache ("./cache"); // you can specify a cache directory.
$ Smarty-> cache_lifetime = 60*60*24; // you can specify the cache time.
$ Smarty-> caching = true; // sets the cache mode.

//----------------------------------------------------
// Left and right boundary characters. the default value is {}.
// Conflict, so it is recommended to set it to <{}> or another.
//----------------------------------------------------
$ Smarty-> left_delimiter = "<{";
$ Smarty-> right_delimiter = "}> ";

$ Smarty-> assign ("name", "Li Xiaojun"); // replace Template variables

// Compile and display the index. tpl template under./templates
$ Smarty-> display ("index. tpl ");
?>

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 author and writing time, which is not required in smarty

Yes, 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 of the current directory.

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 its compilation

Template directory. Note that if the site is located on * nix server, make sure that the directory defined in teamplates_c has the write-readable permission. by default, it is the compiling directory.

Is the templates_c in 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 in practice, because we need to use "script" in the template, the function set in the Script

It is inevitable that {} will be used. although it has its own solution, we are used to redefining it as "<{" and "}>" or" "Or other identifier. Note that if

After the left and right delimiters are defined, each variable must use the same symbol as the definition in the template file. for example, specify <{"and"}> "here ", in the tpl template

{$ Name} is changed 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. Default

It is the cache directory under the current directory, which is equivalent to the templates_c Directory. in * nix system, make sure that 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 its

If the value is-1, the created cache never expires. if the value is 0, the cache is always re-established when the program is executed. 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

Smarty uses the defined cache_lifetime to determine whether to end the cache. 2: indicates that Smarty uses the cache_lifetime value when the cache is created. Habits

Use true or false to indicate whether the cache is performed.

10. $ smarty-> assign ("name", "Li Xiaojun "):
The prototype of this number is assign (string varname, mixed var), varname is the template variable used in the template, var indicates the variable name to replace the template variable; its

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, and all changes to the template

You must use this method to replace the quantity.

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 files here are not

You only need to use a file name to add a path. 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. these directories are the compilation and

Cache directory, which 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.


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 they are also related to the smarty program design.

Some links are very closely related, so I will discuss them 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 when

When the passed array in the program is empty, the following is a simple example:
========================================================== ===
Example6.tpl
========================================================== ===

This is an example of foreach.

Here an array is output:

{Foreach from = $ newsArray item = newsID}
News No.: {$ newsID}

News: {$ newsTitle}

{Foreachelse}
Sorry, there is no news output in the database!
{/Foreach}

========================================================== =
Example6.php
========================================================== =
/*************************************** ******
*
* File name: example6.php
* For use: instance program 2 is displayed.

**************************************** *****/
Include_once ("./comm/Smarty. class. php ");

$ Smarty = new Smarty ();
$ Smarty-> templates ("./templates ");
$ Smarty-> templates_c ("./templates_c ");
$ Smarty-> cache ("./cache ");
$ Smarty-> cache_lifetime = 0;
$ Smarty-> caching = true;
$ Smarty-> left_delimiter = "{";
$ Smarty-> right_delimiter = "}";

$ Array [] = array ("newsID" => 1, "newsTitle" => "1st News ");
$ Array [] = array ("newsID" => 2, "newsTitle" => "2nd news ");
$ Array [] = array ("newsID" => 3, "newsTitle" => "3rd News ");
$ Array [] = array ("newsID" => 4, "newsTitle" => "4th news ");
$ Array [] = array ("newsID" => 5, "newsTitle" => "5th News ");
$ Array [] = array ("newsID" => 6, "newsTitle" => "6th news ");

$ Smarty-> assign ("newsArray", $ array );

// Compile and display the index. tpl template under./templates
$ Smarty-> display ("example6.tpl ");
?>

========================================================== ==========
Example6.php output file
========================================================== ==========

Example of foreach

Here an array is output:

News No.: 1

News: 1st News

News No.: 2

News: 2nd news

News No.: 3

News: 3rd News

News No.: 4

News: 4th News

News No.: 5

News: 5th News

News No.: 6

News: 6th news


Foreach can also use foreachelse to match and use foreachelse to indicate the operations to be performed by the program when the array passed to foreach is null. for specific usage instructions, see

Manual description.

2. section:
Section is used to solve the problem of foreach. like foreach, it is used to design the loop blocks in the template. it is complex and can meet the needs of the program greatly.

I am used to using it in a program instead of using foreach. The basic prototype is:

{Section name = name loop = $ varName [, start = $ start, step = $ step, max = $ max, show = true]}

Name: section name, no need to add $
$ Loop: the variable to be recycled. in the program, use assign to operate the variable.
$ Start: subscript of the start loop. the subscript of the loop starts from 0 by default.
$ Step: increment of the mark in each loop
$ Max: maximum loop subscript
$ Show: boolean type. determines whether to display this block. the default value is true.

Here is a glossary:
Loop subscript: Actually, its English name is index, which means index. here I translate it into "subscript" for better understanding. It indicates that when this loop block is displayed

The previous cyclic index starts from 0 by default and is affected by $ start. if $ start is set to 5, it will also count from 5, we used it in the template design section, which is the current

An attribute of {section}. the call method is Smarty. section. sectionName. index. here, sectionName refers to the name attribute in the function prototype.
The attribute values of the {section} block are:
1. index: the "loop subscript" introduced above. the default value is 0.
2. index_prev: The first value of the current underlying object. the default value is-1.
3. index_next: the next value of the current underlying object. the default value is 1.
4. first: whether it is the first next loop
5. last: whether it is the last loop
6. iteration: number of cycles
7. rownum: current row number, another alias of iteration
8. loop: the last cycle number, which can be used to count the number of cycles of a section after the section block.
9. total: number of cycles. the number of cycles can be counted after the section.
10. show: it is included in the function declaration to determine whether the section is displayed.

For details about their attributes, refer to the manual. you can use these attributes flexibly in the program. I have used the index attribute in the template section.

Similarly, {section} can be used with {sectionelse} to process the template when the input array variable is null.

We use {section} in the above example to replace {foreach} to implement the sample function. Note that in this example, I only use {foreach} in the tpl Template

{Section} is implemented. The php program file is not modified, and the {sectionelse} processing block is added:

========================================================== ===
Example7.tpl
========================================================== ===

This is an example of foreach.

Here an array is output:

{Section name = loop = $ News}
News No.: {$ News [loop]. newsID}

News Title: {$ News [loop]. newsTitle}

{Sectionelse}
Sorry, there is no news input!
{/Section}

========================================================== =
Example6.php
========================================================== =
/*************************************** ******
*
* File name: example7.php
* For use: instance program 2 is displayed.

**************************************** *****/
Include_once ("./comm/Smarty. class. php ");

$ Smarty = new Smarty ();
$ Smarty-> templates ("./templates ");
$ Smarty-> templates_c ("./templates_c ");
$ Smarty-> cache ("./cache ");
$ Smarty-> cache_lifetime = 0;
$ Smarty-> caching = true;
$ Smarty-> left_delimiter = "{";
$ Smarty-> right_delimiter = "}";

$ Array [] = array ("newsID" => 1, "newsTitle" => "1st News ");
$ Array [] = array ("newsID" => 2, "newsTitle" => "2nd news ");
$ Array [] = array ("newsID" => 3, "newsTitle" => "3rd News ");
$ Array [] = array ("newsID" => 4, "newsTitle" => "4th news ");
$ Array [] = array ("newsID" => 5, "newsTitle" => "5th News ");
$ Array [] = array ("newsID" => 6, "newsTitle" => "6th news ");

$ Smarty-> assign ("newsArray", $ array );

// Compile and display the index. tpl template under./templates
$ Smarty-> display ("example6.tpl ");
?>

========================================================== ==========
Example7.php output file
========================================================== ==========

Example of foreach

Here an array is output:

News No.: 1

News: 1st News

News No.: 2

News: 2nd news

News No.: 3

News: 3rd News

News No.: 4

News: 4th News

News No.: 5

News: 5th News

News No.: 6

News: 6th news


Here, the {section} block feels awkward about the variable naming method, but it doesn't matter. you just need to remember the template variable usage:
$ LoopName [name]. in var mode, loopName is the variable name assigned to the loop, and [name] is the string assigned to the name ,. in the program array

The subscript name corresponding to the value is enough.


Well, the smarty Learning Guide-program design is written here. for general applications, this knowledge is enough. for other advanced skills, please refer to the examples in the manual.

In the next section, we will talk about the examples of Smarty in practical applications. we will use the built-in mysql statements in php, the DB classes in phplib, ADODB, the DB class in Pear respectively describes the implementation of each class library in the same example.
 

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.