目錄
3. [代码][PHP]代码
首頁 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);
    }
}

?>
登入後複製

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;);

?>
登入後複製

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>
登入後複製


4. [文件]     test.zip 

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

                                       

                   

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1422
52
Laravel 教程
1316
25
PHP教程
1267
29
C# 教程
1239
24