Initial Experience of the Smarty template in php _ php template _ script house

Source: Internet
Author: User
Today, I started to access the Smarty template, which is attracted by its functional features-design and function separation. In this way, the front-end design and background design can be separated to better implement the division of labor for website development, improve development efficiency.

Today, I started to access the Smarty template, which is attracted by its functional features-design and function separation. In this way, the front-end design and background design can be separated to better implement the division of labor for website development, improve development efficiency.

The following describes the features of the Smarty template engine:

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.

Use the Smarty template versionSmarty-1, 3.0.8, The Directory of the extracted files is as follows:

So I started my Smarty journey --

  Step1

Create a new smartytest folder in the webpage folder on the server. copy the files in the libs directory to the smartytest folder and change the name to smarty.

  Step2

Create a directory templates in test, and create four folders cache, configs, templates, and templates_c under the directory. The folder format is as follows:

  Step3

Write a configuration file to connect to Smarty, in addition, you can write the same code repeatedly when writing different pages to a separate file (of course, you can also write it as a class to facilitate customization ), here, I set the file name to config. php
The Code is as follows:
// Obtain the absolute path H: \ wamp \ www \ smartytest \ of the current folder \
Define ('smarty _ path', substr (dirname (_ FILE _), 0,-9 ));
// Obtain the absolute path of the templates folder H: \ wamp \ www \ smartytest \ templates
Define ('templates _ path', SMARTY_PATH. 'templates /');
Require SMARTY_PATH. 'smarty/smarty. class. php ';
$ Smarty = new Smarty;
// Define the directory path
$ Smarty-> template_dir = TEMPLATES_PATH. 'templates /';
$ Smarty-> complile_dir = TEMPLATES_PATH. 'templates _ c /';
$ Smarty-> config_dir = TEMPLATES_PATH. 'configs /';
$ Smarty-> cache_dir = TEMPLATES_PATH. 'cache /';
// Define the Left and Right terminologies {% and %}
$ Smarty-> left_delimiter = '{% ';
$ Smarty-> right_delimiter = '% }';
// Disable Cache
$ Smarty-> caching = false;
// Disable debugging
$ Smarty-> debugging = false;
?>

Step 4
Write a simple template file named index. tpl and put it in the templates \ templates directory.
The Code is as follows:




Smarty

{% $ Hello %}


Step 5
Write a php file named index. php and put it in the directory of the templates file.
The Code is as follows:
Require 'config. php ';
$ Smarty-> assign ('hello', 'Hello word ');
$ Smarty-> display ('index. tpl ');
?>

The file directory is

  Step6

Test file:

  

Conclusion: problems are also encountered during the use of the Smarty template. For example, you can set the Smarty directory by yourself, which is highly customizable, therefore, there are many versions of the Smarty tutorial (I see one version in the book, and Baidu encyclopedia also has another version). I don't understand how to release the two versions, finally, I used Baidu Encyclopedia (that is, the above mentioned). Secondly, the code in Baidu encyclopedia was copied. As a result, syntax error and unexpected T_VARIABLE error were reported during use, it seems that the code is correct, that is, an error. The final reason is that the space in the webpage is caused by spaces, therefore, the best way to get an inexplicable error of the Code copied on the webpage is to re-write all the spaces. Note that when writing the PHP file, "hello" does not have the $ symbol at the front, the $ symbol must be added to the tpl file.

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.