Quick getting started with smarty

Source: Internet
Author: User

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
========================================================== ==========
<? 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 reality, because we need to use <script> in the template

It is inevitable that {} Will be used. Although it has its own solution, we are used to redefining it as "<{" and "}>" or "<! -- {"And"} --> "or other specifiers. 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
========================================================== ===
<Html>
<Head> <title> This is an example of foreach </title> <Body>
An array will be output here: <br>
{Foreach from = $ newsArray item = newsID}
News No.: {$ newsID} <br>
News: {$ newsTitle} <br> {Foreachelse}
Sorry, there is no news output in the database!
{/Foreach}
</Body>
</Html>

========================================================== =
Example6.php
========================================================== =
<? 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"

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.