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

Source: Internet
Author: User
Tags php template smarty template

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


This example describes the custom min version of the Smarty template engine MinSmarty.class.php file. Share to everyone for your reference, as follows:

First, the advantages of Smarty

Smarty is a template engine written in PHP and is one of the most famous PHP template engines in the industry today. It separates the logic code from the external content, provides an easy-to-manage and useful way to separate the PHP code that was originally mixed with HTML code. To put it simply, the goal is to make the PHP programmer separate from the front-end staff, so that programmers change the logic content of the program does not affect the front-end staff of the page design, the front-end staff to re-modify the page does not affect the program logic, which in the multi-person cooperation project is particularly important.

Two, write a simple Smarty template class

The specific code is as follows:

The path of the <?php class minsmarty{//template file var $template _dir = "./templates/";//the template file is replaced with the file naming format com_ the corresponding tpl.php var $complie _d IR = "./templates_c/"; Store variable value var $tpl _vars = Array (); Here two methods are used to implement assign and display function assign ($tpl _var, $var =null) {if ($tpl _var!=null) {$this->tpl_vars[$tpl _var]=$  var }}//The implementation of the display method is written here function display ($tpl _file) {//Read this template file, replace the PHP file that can be run (post-compile file) $tpl _file_path= $this  Template_dir. $TPL _file; The path to the template file $complie _file_path= $this->complie_dir. " Com_ ". $tpl _file."  PHP ";  The compiled file path//determines if the file exists if (!file_exists ($tpl _file_path)) {return false; }//Do not allow each execution to generate a compile file if (!file_exists ($complie _file_path) | | filemtime ($TPL _file_path) >filemtime ($complie _file_path ) {$fp 1_file_con=file_get_contents ($tpl _file_path);//Get the full contents of the template file///here for a regular replacement replace the code {$title} in the template file with the <?php echo $t his->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, $fp 1_file_con);  Replaced content file_put_contents ($complie _file_path, $new _str); The replaced content generates a PHP file}//introduces the compiled file include_once ("$complie _file_path"); }}?>

The following code is a test of the class

The intro.php code is as follows:

<?php  include_once ("MySmarty.class.php");  $title = "Here is the title";  $content = "Here 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:

 
  {$title}{$content}

PS: Here are recommended several of the site's formatting/beautification/conversion tools can help you tidy up the messy code, I believe that you can use in future development:

PHP Code online format Beautification tool:
Http://tools.jb51.net/code/phpformat

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

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

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

Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson

JSON code online formatting/landscaping/compression/editing/conversion tools:
Http://tools.jb51.net/code/jsoncodeformat

SQL code online Format beautification tool:
Http://tools.jb51.net/code/sqlcodeformat

More interested in smarty related content readers can view the topic: "Smarty Template Primer Basic Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", " PHP Basic Grammar Introductory tutorial, PHP Object-oriented Programming primer, PHP string usage Summary, PHP+MYSQL database Operations Primer and PHP Common database operations Tips Summary

It is hoped that this article will be helpful to everyone based on smarty template PHP program design.

http://www.bkjia.com/PHPjc/1133123.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133123.html techarticle Custom min version smarty template engine MinSmarty.class.php file and usage, Smarty template engine This article describes the custom Min edition smarty template engine MinSmarty.class.php file. Share 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.