PHP template engine smarty

Source: Internet
Author: User
This article mainly introduces the PHP template engine smarty in detail. This article describes the advantages of smarty and smarty, the places that are not suitable for using smarty, And the smarty directory structure and version, for more information, see

This article mainly introduces the PHP template engine smarty in detail. This article describes the advantages of smarty and smarty, the places that are not suitable for using smarty, And the smarty directory structure and version, for more information, see

<? Php
/*
1. What is smarty?

Smarty is a PHP template engine written in PHP. It separates the logic from external content,
The purpose is to separate PHP programmers from the artist. The programmer used to change the logic content of the program does not affect the webpage design of the artist. The engineer re-modifies the page and does not affect the program logic, this is particularly important in projects with multiple partners.

Ii. Advantages of smarty:

1. Speed: Programs Written with smarty can achieve the maximum speed improvement, which is relative to other template engine technologies.

2. compilation type: a program written in smarty must be compiled into a non-template PHP file during runtime. This file adopts a mix of PHP and HTML, during the next template access, the WEB request is directly converted to this file, instead of re-compiling the template (without modifying the source program)

3. cache Technology: A cache technology used by smarty to cache the HTML file that the user sees as a static HTML page. When the cache attribute of smarty is set to true, during the cachetime period set by smarty, user WEB requests are directly converted to this static HTML file, which is equivalent to calling a static HTML file.

4. Plug-in technology: smarty can customize plug-ins. Plug-ins are actually some custom functions.

5. You can use if/elseif/else/endif in the template. Using judgment statements in the template file can easily rearrange the template format.

3. areas not suitable for using smarty:

1. content to be updated in real time. For example, for a stock display, it needs to update data frequently. Using smarty for this type of program slows down the processing speed of the template.

2. small projects. Small projects because the project is simple and the artist and programmer have one project at the same time, using smarty will lose the advantage of rapid php development.


Iv. smarty directory structure and version
Open the official smarty website ,. Download Smarty 3.1.12. Tar.gz and zip are applicable to linux and windows respectively.

After downloading the Smarty-stable-3.1.12 file, decompress it to get a folder named Smarty-3.1.12, which contains two main folders: demo and libs.

The demo folder contains the default folder structure, which is the main folder for programming code. The folder name in the demo is the default directory structure name of smarty. You can change the folder name to the desired name by changing the property value of smarty.
Libs is the smarty code source folder, which generally does not move.

/Libs/Smarty. class. php # main file

/Libs/sysplugins/# Internal plugin

/Libs/plugins/# external plugin, which can be expanded freely

/Demo/cahce/# Place cached files

/Demo/configs/# Place the configuration file that can be loaded

/Demo/templates/# Place the Template File

/Demo/templates_c/# Place the files compiled for the Template

You can change the name of the extracted Smarty-3.1.12 folder to the project name we want, and change the demo to the name of the folder where the encoding is stored.

2. debug Smarty-3.1.12

Create your own file and create index. php In the demo folder.
Create template index. tpl in the templates directory
(It can be the extension of almost any text file, which is commonly used in tpl, php, and html. We do not recommend the latter two because they can be accessed directly from a browser, which is not safe. You can set httpd. conf of apache to prohibit direct access to the. tpl file. Or place the templats directory outside the website document tree .)

*/

// Index. php code
Require ('../libs/Smarty. class. php ');
$ Smarty = new Smarty;
// In the called template, {$ name} can be used to output the name value zhang, and {} is the smarty separator here.
$ Smarty-> assign ('name', 'zhang ');
// The PHP statement block cannot be executed in the template tpl file.
$ Smarty-> display ('templates/index. tpl ');


/*
Index. tpl page content


Hello, {$ name}



*/

/*
The process for Smarty compilation is the source PHP File> template file (which may be called multiple or multiple times)> source PHP file...
That is to say, it does not affect other processing and output of the original PHP file. Therefore, the smarty template file can be a complete html file or a part of it.

Smarty Processing
Smarty first compiles the php source file into an intermediate file (also php). If cache is enabled and then a cache file (also php) is generated based on the compiled file, all the files to be cached are hard-coded.
Each subsequent access will access the compiled file (if the compiled file already exists) and call it multiple times at a time (it can be a single file multiple times or multiple times ), if cache is enabled and cached files are not expired, you can directly access the cached files and skip compiling files.
Once the compilation file is generated, it will not be automatically updated unless the template file or configuration file is changed. Modifying the source PHP file will not cause re-compilation. Once the compiled file is re-generated, the cached file must be re-generated.
*/

// Smarty allows two special compilation settings:
// 1. Do not re-compile automatically at any time (in the launch phase): This file is generated only when there is no compilation file for this file. Changes to the template file or configuration file will not cause re-compilation.
$ Smarty-> setCompile_check (false); // The default value is true. false indicates that no compilation file is generated when the file is changed.
$ Smarty-> getCompile_check (); // get the current compilation check settings
// 2. Re-Compile at any time (debugging phase): Re-Compile at any time.
$ Smarty-> setForce_compile (true); // The default value is false. true indicates re-compilation every time (cache is enabled, and cache is re-cached every time)
$ Smarty-> getForce_compile (); // gets the settings for the current forced Compilation

// Enable Cache
$ Smarty-> setCaching (true );
$ Smarty-> getCaching (); // gets the current cache status. The default value is false.
$ Smarty-> setcache_lifetime (60); // sets the cache time in seconds.
// {* Template file *}
// {Nocache}
// {$ Name}
// {/Nocache}
// {* If cache is enabled, the variables placed in the nocache tag will not be cached. The value of the PHP source file is read each time *}


/*
Smarty delimiter
In a template file, distinguishing between common html code and smarty Code relies on delimiters. The default value is {}, but it may conflict with js and css. Can be changed.
In 3.0, the template tag does not support spaces. For example, {$ abc} can be identified in Smarty2, but it cannot be identified in 3.0. It must be like {$ abc }, this is to better support javascript and css.
*/
$ Smarty-> left_delimiter = "{"; // left delimiter, attribute 2.0, followed by 3.0
$ Smarty-> right_delimiter = "}";
/*
The delimiters are equivalent to PHP echo, and all values in the delimiters are output, unless assigned values or other operations
The content between the two ** operators in the smarty tpl file is the comment content, as shown in figure
Tpl file:
{* This is the template comment *}
*/


// Set the cache directory path. The default "cache" is not set"
$ Smarty-> setCacheDir ("cache ");
// Obtain the cache directory path
$ Smarty-> getCacheDir ();

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.