> php教程 > PHP源码 > 본문

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

PHP中文网
풀어 주다: 2016-05-25 17:10:11
원래의
1430명이 탐색했습니다.

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으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!