PHP template Smarty Specific usage guide _php tutorial

Source: Internet
Author: User
Tags php language php template smarty template
Usually our solution is by the art design page after the delivery of programmers to develop, and then deliver the art to improve the page, back and forth several times, if you encounter the programmer of the HTML is not familiar with the two sides is a painful errand, more inefficient, when the template support is very important.

We know that the PHP language as a member of the open source community, provides a variety of template engine, such as fasttemplate,smarty,simpletemplate, and Smarty is now used more than the PHP template engine, Today and you share in PHP development how to install and use PHP template smarty, but also is the smarty of the introductory learning.

Preparatory work

1. Select the directory to install Smarty

If you have server permissions, consider security you can choose to install smarty outside of the Web program's document directory, and then Smarty install the directory address to include the include_path option in the php.ini file.

If you have virtual host permissions, or several projects, you can install Smarty in your own project directory, in require Smarty class files, or you can use the Smarty template engine. Of course, for security reasons, you can disable directory access through Apache.

In addition, the two types of PHP template Smarty installation is different in terms of portability, the first way to ensure that each server has the same smarty configuration, the second way for each server configuration has no impact. You can choose the Smarty installation method according to your own needs.

2, download smarty,, I choose is Smarty-2.6.25

PHP Template Smarty Installation steps

1. Unzip the downloaded Smarty-2.6.25 compression package

2, copy the Libs folder to the Web program directory, my directory is Testsmarty

The installation method under Linux can be referenced here.

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

PHP Template Smarty Use

1. Create related catalogue

Because in the process of using smarty, Smarty will generate the compiled template file and other configuration files, cache files, we need to create the relevant directory. I created the Tpls directory under the Testsmarty directory and created the templates, Templates_c, configs, and cache directories in the TPLS directory. Why do you need to create these directories? Opening the Smarty.class.php file, we can see that the Smarty class defines the member properties of the section.

$template _dir: Sets the directory address where all template files need to be placed. By default, the directory is: "./templates", that is, in the same directory as the PHP execution program to find the template directory.

$compile _dir: Sets the directory address of all template files Smarty compiled. The default directory is: "./templates_c", that is, in the PHP execution program in the same directory to find the compilation directory. If you create this directory on a Linux server, you also need to modify the permissions on this directory so that it has write permissions.

$config _dir: Set the directory for the special profile of the template, the default directory is: "./configs", that is, in the PHP execution program in the same directory to find the configuration directory.

$cache _dir: In the case of starting the cache feature, the directory specified by this property places the php template smarty all templates cached. The default directory is: "./cache", that is, in the PHP execution program in the same directory to find the cache directory. You can also use your custom cache handler to control the cache file, which will ignore this setting. Also if you create this directory on a Linux server, you also need to modify the permissions of this directory so that it has write permissions.

For system security and portability considerations, it is recommended that you do not build these directories in the same directory as the PHP execution program, but that you can set it up outside of the PHP executable directory, and that you can restrict access to the directory by using Apache if it is already in the same directory as the PHP executable program.

2, establish the relevant configuration file

We need to create a configuration file that overrides the default member property of the PHP template Smarty class and is named Main.php, which is saved in the Smarty directory, and which script needs to use Smarty in the future, as long as we include the main.php in it.

 
 
  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*24;
  11. $TPL- > Left_delimiter = ' < {';
  12. $TPL- > Right_delimiter = '}> ';
  13. ?>

Comments:

第1-8: The main definition of a Smarty object, while setting the template file, compile files, cache files, configuration file directory, overwriting the default values in Smarty.class.php.

第9-10: Set the cache on, and set the cache to a valid time of 1 days.
Knowledge Point: $caching is used to set whether the caching function is turned on. The default value is set to 0 or invalid. You can also have multiple caches for the same template, with a value of 1 or 2 o'clock starting the cache. 1 tells Smarty to use the current $cache_lifetime variable to determine if the cache is out of date. 2 tells PHP template Smarty to use the Cache_lifetime value when generating the cache. We recommend that you close the cache during project development and set the value to 0

第11-12: Set the left and right end of the Smarty language, we know that curly braces are the default delimiters for smarty, but may conflict with JavaScript, CSS, and so on, so here we are set to <{and}>.

3. Create a template file

In general, after the art page design, the intersection of the two sides is a template file, the two sides agreed, the programmer does not need to spend too much effort in the foreground, this is the use of the Smarty template engine development benefits.

We first create a simple template file named Leapsoul.tpl, which you can save as a file of the TPL type after you add the Smarty variable to the HTML file.

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

Note: In this TPL file, the title and content two PHP template Smarty variables are set, the file is saved as LEAPSOUL.TPL, and it is saved in the Testsmartytplstemplates template file directory.

4. Create application files

Template files are similar to a presentation layer, and after we have set up the templates file, we need an application to drive the presentation layer, and the application file is defined as smarty.php.

 
  
  
  1. < ?
  2. Include ("smarty/main.php");
  3. $TPL- > Assign ("title", "Leapsoul.cn for You
    Display Smarty Template technology ");
  4. $TPL- > Assign ("Content", "leapsoul.cn
    Show you the Smarty template technology "with detailed installation and use steps);
  5. $TPL- > display ("Leapsoul.tpl");
  6. ?>

Comments:

In this code we mainly use the smarty in the two functions assign and display,assign you can understand to assign a value to the variable, display is mainly used to output the page. More Smarty functions will be described in more detail in the future.

Other Notes

Since we have opened the cache function, interested friends can open the cache and Templates_c,cache directory to store the cached files of this template, the beginning of the file has cached information, such as the generation of files and expiration time, and other and general HTML file is not much difference, and Templates_c stored the template after the compiled php execution file.

This is an easy-to-get-started PHP template Smarty Application example, even if the presentation is complete.


http://www.bkjia.com/PHPjc/446267.html www.bkjia.com true http://www.bkjia.com/PHPjc/446267.html techarticle usually our solution is by the art design page after the delivery of programmers to develop, and then deliver the art to improve the page, back and forth several times, if you encounter the program designer to ...

  • 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.