Smarty Half-hour quick start tutorial, Smarty Quick Get started _php tutorial

Source: Internet
Author: User
Tags php language php template smarty template

Smarty Half-hour quick start tutorial, Smarty get started quickly


This article describes a quick-start approach to Smarty, which allows readers to quickly master the use of smarty in half an hour. Share to everyone for your reference. The implementation method is as follows:

First, the Smarty programming part:

In the Smarty template Design section I simply put Smarty in the template of some common settings to do a simple introduction, this section mainly to introduce how to start our program design in Smarty. Download the Smarty file and put it on your site.

The index.php code is as follows:

Copy the Code code as follows: <?php
/**
*
* @version $Id: index.php
* @package
* @author www.jb51.net
* @action Show Instance Program
*/
Include_once ("./smarty/smarty.class.php"); Contains the Smarty class file

$smarty = new Smarty (); Establish the Smarty instance object $smarty
$smarty->templates ("./templates"); Set up a template directory
$smarty->templates_c ("./templates_c"); Set compilation directory
$smarty->cache ("./cache"); Cache Directory
$smarty->cache_lifetime = 0; Cache time
$smarty->caching = true; Cache mode

$smarty->left_delimiter = "{#";
$smarty->right_delimiter = "#}";
$smarty->assign ("name", "Zaocha"); Make template variable substitution
$smarty->display ("index.htm"); Compile and display the index.htm template located under./templates
?>
Ii. Procedures for Interpreting Smarty

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

1:/**/statement:

The included section is the program header comment. The main content should be the role of the program, copyright and author and writing time to do a simple introduction, which is not required in smarty, but from the procedural style, this is a good style.

2:include_once statement:

It will install the Smarty file to the Web site into 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 using the TPL template, it is a directory, in the absence of this sentence, smarty the default template path is the templates directory of the current directory, the actual writing process, we will write this sentence, this is a good program style.

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

This sentence indicates the directory at which the $smarty object was compiled. In the template design we already know that Smarty is a compiled template language, and this directory is the directory where it compiles templates, note that if the site is located on a Linux server, make sure

The directory defined in Teamplates_c has a writable and readable permission, and by default its compilation directory is the Templates_c of the current directory, and for the same reason we write it explicitly.

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

Indicates the left and right delimiters when a template variable is found. "{" and "}" by default, but in practice because we want to use it in the template

Related Article

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.