Edit a small Smarty class

Source: Internet
Author: User
Tags php template

First set up two folders, a temp, store the pre-compiled files, a comp, store the compiled file, the pre-compilation file using {$title} instead of the <?php echo $title;, and then compile the former into the latter, After parsing is stored in the Comp folder

Define a parsing class smarty.class.php

<?php//defines a simple Smarty class to compile the HTML file class mini{public $temp _uri= ";//Where the template file is located Public $comp _uri= ';//The location of the template after compiling public $_var=array ();//variable array public function assign ($key, $value) {$this->_    var[$key]= $value;        Public function display ($temp) {//Package $uri = $this->complie ($temp);    Require ($uri); }/* String $tempplate template file name return String */Public Function Complie ($temp) {//read out the contents of the template $te =$ This->temp_uri. '        /'. $temp;//Pre-compiled files $source =file_get_contents ($te); $comp = './comp/'. $temp.        PHP ';            Determine if this file is present if (file_exists ($comp) &&filemtime ($comp) >filemtime ($te)) {//post-compilation file exists and will be saved earlier than the pre-compilation file        return $comp;        } $source =str_replace (' {$ ', ' <?php echo $this->_var[\ ', $source); $source =str_replace ('} ', ' \ ');?                > ', $source);        Var_dump (File_put_contents ($comp, $source));    return $comp; }}?> 

Pre-compilation file in temp directory 01temp.html

<!doctype html>

Control Page 01.php

<?phprequire ('./smarty.class.php '); $title = ' mall '; $content = ' This is my mall oh '; $smarty =new mini (); $smarty->temp_uri= '. /temp '; $smarty->comp_uri= './comp ';//$smarty->complie (' 01temp.html '); $smarty->assign (' title ', $title); $ Smarty->assign (' content ', $content); $smarty->display (' 01temp.html ');? >

Run the 01.php build post-compilation file in the comp directory 01temp.html.php

<!doctype html>

Achieve the right results

Edit a small Smarty class

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.