ホームページ > php教程 > PHP源码 > 超级精简的php模板解析引擎 iSmarty

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

PHP中文网
リリース: 2016-05-25 17:10:11
オリジナル
1493 人が閲覧しました

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

                                       

                   

ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のおすすめ
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート