This example describes the custom min version of the Smarty template engine MinSmarty.class.php file. Share to everyone for your reference, specific as follows:
First, the advantages of Smarty
Smarty is a template engine written using PHP and is one of the most famous PHP template engines in the industry. It separates the logical code from the external content, provides an easy to manage and use method to separate the PHP code from the original HTML code. Simply speaking, the goal is to make the PHP programmer with the front-end staff separation, so that the programmer changes the logic of the program will not affect the front-end staff of the page design, the front-end staff to modify the page does not affect the program logic, which in many people cooperation project is particularly important.
Second, write a simple Smarty template class
The specific code is as follows:
<?php class minsmarty{//Stencil file path var $template _dir = "./templates/";
Template file is replaced by the file naming format for com_ corresponding tpl.php var $complie _dir = "./templates_c/";
Store variable value var $tpl _vars = Array (); Here are two methods for implementing assign and display function assign ($tpl _var, $var =null) {if ($tpl _var!=null) {$this->tpl_vars[$tpl _var]=$
var }//Here write Display method implementation function display ($TPL _file) {//Read this template file-> replace the PHP file that can be run (compiled file) $tpl _file_path= $this->t Emplate_dir. $TPL _file; The path to the stencil file $complie _file_path= $this->complie_dir. " Com_ ". $tpl _file.". PHP ";
The compiled file path//decision file exists if (!file_exists ($tpl _file_path)) {return false; ///Do not allow compile files to be generated for each execution 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 entire contents of the template file//To 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, $fp 1_file_con); After the replacement of the content file_put_contents ($complie _file_path, $new _str);
Replace the content to generate a PHP file}///Introduce 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 the 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: Here recommend a few of the site's format/landscaping/conversion tools to help you organize the messy code, I believe that we can use in future development:
PHP code online format Landscaping tools:
Http://tools.jb51.net/code/phpformat
JavaScript code Landscaping/compression/formatting/encryption Tools:
http://tools.jb51.net/code/jscompress
Online XML format/compression tools:
Http://tools.jb51.net/code/xmlformat
JSON Code Formatting Landscaping tool:
Http://tools.jb51.net/code/json
Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson
JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat
SQL code Online formatting Landscaping tools:
Http://tools.jb51.net/code/sqlcodeformat
More interested in smarty related content readers can view the site topics: "Smarty Template Primer Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", " Introduction to PHP Basic Grammar, "Introduction to PHP object-oriented programming", "PHP string (String) Usage Summary", "Php+mysql Database Operations Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with your PHP programming based on Smarty templates.