PHP template-Smarty tutorial

Source: Internet
Author: User
Tags php template smarty template

We knowPHPAs a member of the open-source community, the language provides various template engines, such as FastTemplate,SmartyAnd SimpleTemplate, and Smarty is a PHP template engine that is currently used a lot. Today we will share with you how to install and use Smarty in PHP development, which is also a quick start to Smarty.

I. Preparations

1. Select the directory for installing Smarty

If you have server permissions, you can choose to install Smarty outside the WEB application documentation directory, and then include the Smarty installation directory address in the include_path option in the PHP. ini file.

If it is a virtual host permission or several projects, you can install Smarty in their respective project directories, In the require Smarty file, or use the Smarty template engine. For the sake of security, you can use apache to disable access to related directories.

In addition, the two Smarty installation methods differ in portability. The first method requires that each server has the same Smarty configuration. The second method does not affect the configuration of each server. You can select the Smarty installation method as needed.

2. Download Smarty. Click here to download Smarty. I chose Smarty-2.6.25.

Install Smarty

1. decompress the downloaded Smarty-2.6.25 package

2. Copy the libs folder to the WEB program directory. My directory is testsmarty.

For installation methods in Linux, refer to here.

After installing the Smarty template, we started to use the Smarty.

Iii. Use Smarty

1. Create related directories

Because in the process of using Smarty, Smarty generates compiled template files, other configuration files, and cache files, we need to create related directories. Under the testsmarty directory, I also created the tpls directory and the templates, templates_c, configs, and cache directories under the tpls directory. Why do we need to create these directories? Open the Smarty. class. php file. We can see that the Smarty class defines some member attributes.

$ Template_dir: Specifies the directory address to be placed in all template files. By default, the directory is "./templates", that is, find the template directory under the same directory of the PHP Execution program.

$ Compile_dir: Specifies the directory where all template files compiled by Smarty are stored. The default directory is "./templates_c", that is, find the compiling directory under the same directory of the PHP Execution program. If you create this directory on a Linux server, you also need to modify the directory permission so that it has the write permission.

$ Config_dir: Set the directory used to store the special configuration files of the template. The default directory is "./configs", that is, find the configuration directory under the same directory of the PHP Execution program.

$ Cache_dir: When the cache feature is enabled, all templates cached by Smarty are placed in the directory specified by this attribute. The default directory is "./cache", that is, to find the cache directory under the same directory of the PHP Execution program. You can also use your custom cache handler to control cached files, which will ignore this setting. Similarly, if you create this directory on a Linux server, you also need to modify the directory permission so that it has the write permission.

For the sake of system security and portability, we recommend that you do not create these directories in the same directory of the PHP Execution program. You can create these directories in addition to the PHP Execution program directory, if you have already created a PHP Execution program in the same directory, you can use Apache to restrict access to directories.

2. Create related configuration files

We need to create a configuration file to overwrite the default member attributes of the Smarty class and name it main. php, saved in the smarty Directory, which script needs to use Smarty in the future, we just need to put main. include php.

 
 
  1. <?  
  2. include("./smarty/libs/Smarty.class.php");  
  3. define('SMARTY_ROOT', './smarty/tpls');  
  4. $tpl = new Smarty();  
  5. $tpl->template_dir = SMARTY_ROOT."/templates/";  
  6. $tpl->compile_dir = SMARTY_ROOT."/templates_c/";  
  7. $tpl->config_dir = SMARTY_ROOT."/configs/";  
  8. $tpl->cache_dir = SMARTY_ROOT."/cache/";  
  9. $tpl->caching=1;  
  10. $tpl->cache_lifetime=60*60*24;  
  11. $tpl->left_delimiter = '<{';  
  12. $tpl->right_delimiter = '}>';  
  13. ?> 

Note:

Line 1-8: defines a smarty object, and sets the directory for storing template files, compilation files, cache files, and configuration files to overwrite the default values in Smarty. class. php.

Row 9-10: set to enable the cache and set the cache validity period to 1 day.

Knowledge Point: $ caching is used to set whether the cache function is enabled. The default value is 0 or invalid. You can also set multiple caches for the same template. When the value is 1 or 2, the cache is started. 1. Tell Smarty to use the current $ cache_lifetime variable to determine whether the cache has expired. 2. Tell Smarty to use the cache_lifetime value when the cache is generated. We recommend that you disable caching during project development and set the value to 0.

Line 11-12: Set the Left and Right terminator of the smarty language. We know that braces are the default delimiters of the smarty language, but they may conflict when combined with javascript and css, so here we set it to <{And}>.

3. Create a template file

Generally, after the design of the meigong page is completed, the intersection of the two parties is a template file. after both parties agree, programmers do not need to spend too much energy on the front-end, this is the benefit of using the Smarty template engine for development.

First, create a simple template file named leapsoul. tpl. You can add the smarty variable to the html file and save the file as a tpl file.

 
 
  1. <meta http-equiv="Content-type" content="text/html; charset=gb2312"> 
  2. <title>   
  3. <{ $title }> 
  4. </title>   
  5. <body>   
  6. <{ $content }>   
  7. </body>   

Note: Two smarty variables, title and content, are set in the tpl file. The files are saved as leapsoul. tpl and saved in the directory of the testsmartytplstemplates template file.

4. Create an application file

The template file is similar to a presentation layer. After the template file is created, we need an application to drive the presentation layer. The application file is defined as smarty. php.

 
 
  1. <?
  2. Include ("smarty/main. php ");
  3. $ Tpl-> assign ("title", "leapsoul.cn: smarty template technology ");
  4. $ Tpl-> assign ("content", "leapsoul.cn demonstrate the smarty template technology through detailed installation and usage steps ");
  5. $ Tpl-> display ("leapsoul. tpl ");
  6. ?>

Note:

In this Code, we mainly use the two functions assign and display in smarty. assign can be understood as a variable assignment, and display is mainly used to output webpages. More smarty functions will be introduced in detail in the future.

Other Instructions

Since we have enabled the cache function, interested friends can open cache and templates_c. The cache directory stores the cached files of this template. The file starts with cache information, for example, the file generation time and expiration time are not much different from other HTML files, while templates_c stores the PHP Execution file compiled by the template.

Now, the introduction of a simple getting started Smarty template application instance is complete.

Original article address:Http://www.leapsoul.cn /? P = 405


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.