Smarty Example Tutorial---programming part

Source: Internet
Author: User
Tags php language php file php code readable smarty template

The programming part of Smarty:

In the Smarty template Design section I simply put the smarty in the template of some common settings do a simple introduction, this section mainly to introduce how to start in the smarty of our journey

Order design.

PHP Code:--------------------------------------------------------------------------------

Let's begin by introducing some of the elements in the. php file that we used in the previous section. Again, let's take the first index.php file in the previous section to illustrate:

================================================

index.php

================================================

<?php
/*********************************************
*
* FileName: index.php
* Role: Show Instance Program
*
* Author: Big bro
* email:teacherli@163.com
*
*********************************************/
Include_once ("./comm/smarty.class.php"); Contains Smarty class files
$smarty = new Smarty (); Establish a Smarty instance object $smarty
$smarty->templates ("./templates"); Set up template catalogs
$smarty->templates_c ("./templates_c"); Setting up the compilation directory
Everybody, this is my new ****//.
$smarty->cache ("./cache"); Set Cache Directory
$smarty->cache_lifetime = 60 * 60 * 24; Set Cache time
$smarty->caching = true; Set caching mode
//----------------------------------------------------
The left and right border characters, the default is {}, but the actual application is easy with javascrīpt
Conflict, it is recommended to set <{}> or other.
//----------------------------------------------------
$smarty->left_delimiter = "<{";
$smarty->right_delimiter = "}>";
$smarty->assign ("name", "Li Xiaojun"); Make template variable substitution
Compile and display the Index.tpl template that is located under./templates
$smarty->display ("Index.tpl");
?>

We can see that the Smarty part of the program is actually a set of code that conforms to the PHP language specification, which we explain in turn:

1. /**/statement:

The included section is a program header comment. The main content should be the role of the program, copyright and the author and writing time to do a simple introduction, which in the smarty is not necessarily

Required, but in terms of the program style, this is a good style.

2. Include_once statement:

It will be installed to the Web site Smarty file included in the current file, note that the included path must be written correctly.

3. $smarty = new Smarty ():

This sentence creates a new Smarty object $smarty, a simple instantiation of an object.

4. $smarty->templates (""):

This sentence indicates the path of the $smarty object when it uses the TPL template, which is a directory where, in the absence of this sentence, smarty the default template path to the current directory templates

directory, actually in writing the program, we want to write this sentence, this is a good program style.

5. $smarty->templates_c (""):

This sentence indicates the directory at compile time for the $smarty object. In the template design article we already know that Smarty is a compiled template language, and this directory is where it compiles

Template directory, note that if the site is located on the *nix server, make sure that the directory defined in Teamplates_c has writable readable permissions, and by default its compilation directory

Is the Templates_c in the current directory, and for the same reason we write it out clearly.

6. $smarty->left_delimiter and $smarty->right_delimiter:

Indicates the left and right delimiters when looking for template variables. The default is "{" and "}", but in practice because we want to use the function in <scrīpt>,scrīpt in the template to set

Righteousness will inevitably use {}, although it has its own solution, but it is customary to redefine it as "<{" and "}>" or "<!--{" and "}-->" or other markers, note that if here

Once the left and right delimiters are defined, in the template file, you want each variable to use the same symbol as the definition, such as "<{" and "}>", in the TPL template.

{$name} becomes <{$name}>, so that the program can correctly locate the template variable.

7. $smarty->cache ("./cache"):

Tells Smarty the location of the template file cache for output. We know that the biggest advantage of smarty is that it can be cached, and here is the directory where the cache is set. Default Love

Condition is the cache directory under the current directory, equivalent to the Templates_c directory, in the *nix system we want to ensure its readable and writable.

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

This will be the time in seconds for the calculation of the cache to be valid. When the first cache time expires, the cache is rebuilt when the Smarty caching variable is set to True. When it's

A value of 1 indicates that the established cache never expires, and 0 indicates that the cache is always being reset every time the program executes. The above setting indicates that the Cache_lifetime is set to one day.

9. $smarty->caching = 1:

This property tells Smarty whether to cache and how to cache it. It can take 3 values, 0:smarty the default value, and indicates that the template is not cached; 1: Indicates

Smarty will use the currently defined cache_lifetime to decide whether to end the cache;2: The Smarty will use the Cache_lifetime value when the cache is built. The habit of making

Use True and false to indicate whether caching is done.

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.