PHP Custom Smarty template engine class Gaulo PHP

Source: Internet
Author: User
Tags vars smarty template

Smarty template engine class simple working principle

Using the Smarty template engine class to compile the variables in the template file, the compilation process is actually translated into PHP files using regular expressions. For example, in the template file {$title} use regular expressions to find and replace <?php echo $this->vars[' title '];? >

Customizing the Smarty Template engine Class smarty.class.php page

<?php/* *  Custom Smarty template engine class  */        class smarty {            private  $vars  = array ( );             //the first assignment of variables to a template               //has two parameters   one parameter is the variable name in the template, and the value of the variable assigned to it              public function assign ($name, $value =null ) {                         if ($name  !=  '   ')                                    $this->vars[$name]= $value;             }                         //loading the specified template   and displaying              //has a parameter that is the file name of the template              public function display ($tplname) {                          $comfile  =   "./comps/". $tplname. " _com.php ";                          $tplname  =  "./templates/". $tplname;                          //compile file does not exist   or template file changes   need to compile           &nbSp;              if (!file_exists ($ Comfile)  | |  filemtime ($tplname) >filemtime ($comfile)) {                          $html  = file_get _contents ($tplname);                                                  //the part {title} to replace                           $reg  =  '/\{\s*\$ ([a-za-z_\x7f-\xff][a-za-z0-9_\x7f-\xff]*) \s*\}/ ';                         //variable Regular expression [a-za-z_\x7f-\xff][a-za-z0-9_\x7f-\xff]*                                                    //Replacement part <?php echo  $this->vars[' title ';? >                          $rep  =  "<?php echo \ $this->vars[' \\1 ');? > ";                                                   $newhtml  = preg_ Replace ($reg,  $rep,  $html);                              file_put_contents ($comfile,  $newhtml);                          }                         include  $comfile;             }                     }

Invoke Template page index.php

<?phpheader (' content-type:text/html;charset=utf-8 '); */* Template engine * PHP hypertext preprocessing scripting language * Custom Template engine * * *//Include templates engine class include ' smart . class.php '; Create template Engine Object $smarty = new Smarty (); Connect database//Execute SQL statement//This is the dynamic data obtained from the database and needs to be displayed in the template $title = "This is a test";  $content = "This is content ..."; The first assigns a variable to the template $smarty->assign (' title ', $title); $smarty->assign (' content ', $content); Var_dump ($smarty); Loads the specified template and displays the $smarty->display (' c.php ');

Template File Page c.php page

Output results

Object (Smarty) [1] private ' vars ' = = Array (size=2) ' title ' = = String ' This is a test ' (length=14) ' Co Ntent ' = = String ' This is content ... ' (length=22) This was a testthis is content ...


This article is from the "Jin Sha Harbor" blog, please be sure to keep this source http://11410485.blog.51cto.com/11400485/1843240

PHP Custom Smarty template engine class Gaulo PHP

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.