Custom min version smarty template engine MinSmarty. class. php file and usage, smarty template engine

Source: Internet
Author: User

Custom min version smarty template engine MinSmarty. class. php file and usage, smarty template engine

This article describes the custom min version of the smarty template engine MinSmarty. class. php file. We will share this with you for your reference. The details are as follows:

I. Advantages of smarty

Smarty is a template engine written in PHP and one of the most famous PHP template engines in the industry. It separates the logic code from the external content and provides an easy-to-manage and use method to combine the originally HTML code with the PHP code logic separation. Simply put, the purpose is to separate PHP programmers from front-end personnel so that programmers can change the logic content of the program without affecting the front-end personnel's page design, the re-modification of the page by the front-end personnel does not affect the program logic, which is particularly important in projects with multiple partners.

2. Write a simple smarty template class

The Code is as follows:

<? Php class MinSmarty {// path of the template file var $ template_dir = ". /templates/"; // After the template file is replaced, the file name format is tpl corresponding to com. php var $ complie_dir = ". /templates_c/"; // store the variable value var $ tpl_vars = array (); // use two methods to implement assign and display function assign ($ tpl_var, $ var = NULL) {if ($ tpl_var! = NULL) {$ this-> tpl_vars [$ tpl_var] = $ var ;}// compile the display method implementation function display ($ tpl_file) {// read this template file-> Replace the PHP file that can be run (the compiled file) $ tpl_file_path = $ this-> template_dir. $ tpl_file; // template file path $ complie_file_path = $ this-> complie_dir. "com _". $ tpl_file. ". php "; // The compiled file path // determine whether the file exists if (! File_exists ($ tpl_file_path) {return false;} // do not let every execution generate the compilation file if (! File_exists ($ complie_file_path) | filemtime ($ tpl_file_path)> filemtime ($ complie_file_path) {$ fp1_file_con = file_get_contents ($ tpl_file_path ); // obtain all the content of the template file. // Replace the code {$ title} in the template file with <? Php echo $ this-> tpl_vars ['title'];?> $ Pattern = array ('/\ {\ s * \ $ ([a-zA-Z _] [a-zA-Z0-9 _] *) \ s * \}/I '); $ replace = array ('<? Php echo $ this-> tpl_vars ["$ {1}"];?> '); $ New_str = preg_replace ($ pattern, $ replace, $ fp1_file_con); // The replaced content file_put_contents ($ complie_file_path, $ new_str ); // generate a PHP file for the replaced content} // introduce the compiled file include_once ("$ complie_file_path") ;}}?>

The following code tests the class.

The intro. php code is as follows:

<? Php include_once ("MySmarty. class. php "); $ title =" here is the title "; $ content =" this is content 111111 "; $ MySmarty = new MySmarty (); $ MySmarty-> assign ("title", $ title); $ MySmarty-> assign ("content", $ content); $ MySmarty-> display ("intro. tpl ");?>

The template is as follows:

Intro. tpl:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

PS: We recommend several formatting, beautification, and conversion tools on this site to help you sort out messy code. I believe you can use them in future development:

Php code online formatting and beautification tools:
Http://tools.jb51.net/code/phpformat

JavaScript code beautification/compression/formatting/encryption tools:
Http://tools.jb51.net/code/jscompress

Online XML formatting/compression tools:
Http://tools.jb51.net/code/xmlformat

JSON code formatting and beautification tools:
Http://tools.jb51.net/code/json

Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson

Json code online formatting/beautification/compression/editing/conversion tools:
Http://tools.jb51.net/code/jsoncodeformat

SQL code online formatting and beautification tools:
Http://tools.jb51.net/code/sqlcodeformat

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.