Table of Contents
3. [代码][PHP]代码
Home php教程 PHP源码 超级精简的php模板解析引擎 iSmarty

超级精简的php模板解析引擎 iSmarty

May 25, 2016 pm 05:10 PM

1. [代码][PHP]代码    

<?php
class iSmarty
{
    /*
     * The name of the directory where templates are located.
     * @var string
     */
    var $templatedir = &#39;/&#39;;
    
    /*
     * The directory where compiled templates are located.
     * @var string
     */
    var $compiledir = &#39;test/&#39;;
    
    /*
     * where assigned template vars are kept
     * @var array
     */
    var $vars = array();
    
    /*
     * compile a resource
     * sets PHP tag to the compiled source
     * @param string $tpl (template file)
     */
    function parse($tpl)
    {
        // load template file //
        $fp   = @fopen($tpl, &#39;r&#39;);
        $text = fread($fp, filesize($tpl));
        fclose($fp);
        // repalce template tag to PHP tag //
        $text        = str_replace(&#39;{/if}&#39;, &#39;<?php } ?>&#39;, $text);
        $text        = str_replace(&#39;{/loop}&#39;, &#39;<?php } ?>&#39;, $text);
        $text        = str_replace(&#39;{foreachelse}&#39;, &#39;<?php } else {?>&#39;, $text);
        $text        = str_replace(&#39;{/foreach}&#39;, &#39;<?php } ?>&#39;, $text);
        $text        = str_replace(&#39;{else}&#39;, &#39;<?php } else {?>&#39;, $text);
        $text        = str_replace(&#39;{loopelse}&#39;, &#39;<?php } else {?>&#39;, $text);
        // template pattern tags //
        $pattern     = array(
            &#39;/\$(\w*[a-zA-Z0-9_])/&#39;,
            &#39;/\$this\-\>vars\[\&#39;(\w*[a-zA-Z0-9_])\&#39;\]+\.(\w*[a-zA-Z0-9])/&#39;,
            &#39;/\{include file=(\"|\&#39;|)(\w*[a-zA-Z0-9_\.][a-zA-Z]\w*)(\"|\&#39;|)\}/&#39;,
            &#39;/\{\$this\-\>vars(\[\&#39;(\w*[a-zA-Z0-9_])\&#39;\])(\[\&#39;(\w*[a-zA-Z0-9_])\&#39;\])?\}/&#39;,
            &#39;/\{if (.*?)\}/&#39;,
            &#39;/\{elseif (.*?)\}/&#39;,
            &#39;/\{loop \$(.*) as (\w*[a-zA-Z0-9_])\}/&#39;,
            &#39;/\{foreach \$(.*) (\w*[a-zA-Z0-9_])\=\>(\w*[a-zA-Z0-9_])\}/&#39;
        );
        // replacement PHP tags //
        $replacement = array(
            &#39;$this->vars[\&#39;\1\&#39;]&#39;,
            &#39;$this->vars[\&#39;\1\&#39;][\&#39;\2\&#39;]&#39;,
            &#39;<?php $this->display(\&#39;\2\&#39;)?>&#39;,
            &#39;<?php echo \$this->vars\1\3?>&#39;,
            &#39;<?php if(\1) {?>&#39;,
            &#39;<?php } elseif(\1) {?>&#39;,
            &#39;<?php if (count((array)\$\1)) foreach((array)\$\1 as \$this->vars[\&#39;\2\&#39;]) {?>&#39;,
            &#39;<?php if (count((array)\$\1)) foreach((array)\$\1 as \$this->vars[\&#39;\2\&#39;]=>$this->vars[\&#39;\3\&#39;]) {?>&#39;
        );
        // repalce template tags to PHP tags //
        $text = preg_replace($pattern, $replacement, $text);
        
        // create compile file //
        $compliefile = $this->compiledir . basename($tpl) . &#39;.php&#39;;
        if ($fp = @fopen($compliefile, &#39;w&#39;)) {
            fputs($fp, $text);
            fclose($fp);
        }
    }
    
    /*
     * assigns values to template variables
     * @param array|string $k the template variable name(s)
     * @param mixed $v the value to assign
     */
    function assign($k, $v = null)
    {
        $this->vars[$k] = $v;
    }
    
    /*
     * ste directory where templates are located
     * @param string $str (path)
     */
    function templateDir($path)
    {
        $this->templatedir = $this->pathCheck($path);
    }
    
    /*
     * set where compiled templates are located
     * @param string $str (path)
     */
    function compileDir($path)
    {
        $this->compiledir = $this->pathCheck($path);
    }
    
    /*
     * check the path last character
     * @param string $str (path)
     * @return string
     */
    function pathCheck($str)
    {
        return (preg_match(&#39;/\/$/&#39;, $str)) ? $str : $str . &#39;/&#39;;
    }
    
    /*
     * executes & displays the template results
     * @param string $tpl (template file)
     */
    function display($tpl)
    {
        $tplfile = $this->templatedir . $tpl;
        if (!file_exists($tplfile)) {
            exit(&#39;can not load template file : &#39; . $tplfile);
        }
        $compliefile = $this->compiledir . $tpl . &#39;.php&#39;;
        if (!file_exists($compliefile) || filemtime($tplfile) > filemtime($compliefile)) {
            $this->parse($tplfile);
        }
        include_once($compliefile);
    }
}

?>
Copy after login

2. [代码][PHP]代码

<?php
include &#39;iSmarty.php&#39;;
$tpl = new iSmarty();
$tpl->assign(&#39;title&#39;,&#39;标题&#39;);
$tpl->assign(&#39;Name&#39;,&#39;名字&#39;);

$contact = array(&#39;1&#39;=>&#39;张一&#39;,&#39;2&#39;=>&#39;zhang2&#39;);
$tpl->assign(&#39;contact&#39;,$contact);
$tpl->display(&#39;test.html&#39;);

?>
Copy after login

3. [代码][PHP]代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<p>{$title} - {$Name}</p>
{foreach $contact  key=>val}
	{$key}: {$val}<br>
{/foreach}
</body>
</html>
Copy after login


4. [文件]     test.zip 

超级精简的php模板解析引擎 iSmartytest.zip

                                       

                   

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24