首页 >后端开发 >php教程 >php模板引擎技术简单实现,php模板引擎_PHP教程

php模板引擎技术简单实现,php模板引擎_PHP教程

WBOY
WBOY原创
2016-07-12 08:57:36898浏览

php模板引擎技术简单实现,php模板引擎

用了smarty,tp过后,也想了解了解其模板技术是怎么实现,于是写一个简单的模板类,大致就是读取模板文件->替换模板文件的内容->保存或者静态化

 

tpl.class.php主要解析

  assign 方法实现

<span>        /*</span><span>*
          * 模板赋值操作
          * @param mixed $tpl_var 如果是字符串,就作为数组索引,如果是数组,就循环赋值
          * @param mixed $tpl_value 当$tpl_var为string时的值,默认为 null
          </span><span>*/</span>
        <span>public</span> function assign($tpl_var,$tpl_value=<span>null</span><span>){
            </span><span>if</span>(is_array($tpl_var) && count($tpl_var) > <span>0</span><span>){
                </span><span>foreach</span> ($tpl_var <span>as</span> $k =><span> $v) {
                    $</span><span>this</span>->tpl_vars[$k] =<span> $v;
                }
            }elseif($tpl_var){
                $</span><span>this</span>->tpl_vars[$tpl_var] =<span> $tpl_value;
            }
        }</span>

fetch 方法实现

         <span>/*</span><span>*
           * 生成编译文件
           * @param string $tplFile 模板路径
           * @param string $comFile 编译路径
           * @return string
          </span><span>*/</span>
        <span>private</span><span> function fetch($tplFile,$comFile){

            </span><span>//</span><span>判断编译文件是否需要重新生成(编译文件是否存在或者模板文件修改时间大于编译文件的修改时间)</span>
            <span>if</span>(!file_exists($comFile) || filemtime($tplFile) ><span> filemtime($comFile)){
                </span><span>//</span><span>编译,此处也可以使用ob_start()进行静态化</span>
                $content = $<span>this</span>-><span>tplReplace(file_get_contents($tplFile));
                file_put_contents($comFile, $content);
            }

        }            </span>

简单编译方法:按照规则进行正则替换

        <span>/*</span><span>*
          * 编译文件
          * @param string $content 待编译的内容
          * @return string
          </span><span>*/</span>
        <span>private</span><span> function tplReplace($content){
            </span><span>//</span><span>转义左右定界符 正则表达式字符</span>
            $left = preg_quote($<span>this</span>->left_delimiter,<span>'</span><span>/</span><span>'</span><span>);
            $right </span>= preg_quote($<span>this</span>->right_delimiter,<span>'</span><span>/</span><span>'</span><span>);

            </span><span>//</span><span>简单模拟编译 变量</span>
            $pattern =<span> array(
                    </span><span>//例如</span><span>{$test}</span>
                    <span>'</span><span>/</span><span>'</span>.$left.<span>'</span><span>\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)</span><span>'</span>.$right.<span>'</span><span>/i</span><span>'</span><span>
                );

            $replace </span>=<span> array(
                    </span><span>'</span><span><?php echo $this->tpl_vars[\'${1}\']; ?></span><span>'</span><span>
                );

            </span><span>//</span><span>正则处理</span>
            <span>return</span><span> preg_replace($pattern, $replace, $content);
        }</span>

display = fetch+echo

        <span>/*</span><span>*
          * 输出内容
          * @param string $fileName 模板文件名
          </span><span>*/</span>
        <span>public</span><span> function display($fileName){
            </span><span>//</span><span>模板路径</span>
            $tplFile = $<span>this</span>->template_dir.<span>'</span><span>/</span><span>'</span><span>.$fileName;

            </span><span>//</span><span>判断模板是否存在</span>
            <span>if</span>(!<span>file_exists($tplFile)){
                $</span><span>this</span>->errorMessage = <span>'</span><span>模板文件不存在</span><span>'</span><span>;
                </span><span>return</span> <span>false</span><span>;
            }

            </span><span>//</span><span>编译后的文件</span>
            $comFile = $<span>this</span>->compile_dir.<span>'</span><span>/</span><span>'</span>.md5($fileName).<span>'</span><span>.php</span><span>'</span><span>;

            $</span><span>this</span>-><span>fetch($tplFile,$comFile);
            <br />       include $comFile;
        }</span>

 

其他属性

<span>        //</span><span>模板文件存放位置</span>
        <span>private</span> $template_dir = <span>'</span><span>templates</span><span>'</span><span>; 

        </span><span>//</span><span>编译文件存放位置</span>
        <span>private</span> $compile_dir = <span>'</span><span>compiles</span><span>'</span><span>;

        </span><span>//</span><span>左定界符</span>
        <span>private</span> $left_delimiter = <span>'</span><span>{</span><span>'</span><span>;

        </span><span>//</span><span>右定界符 </span>
        <span>private</span> $right_delimiter = <span>'</span><span>}</span><span>'</span><span>; 

        </span><span>//</span><span>内部临时变量,存储用户赋值</span>
        <span>private</span> $tpl_vars =<span> array();

        </span><span>//</span><span>错误信息</span>
        <span>private</span> $errorMessage = <span>''</span><span>;

        </span><span>/*</span><span>*
          * 修改类属性的值
          * @param array $configs 需要修改的相关属性及值
          * @return bool
          </span><span>*/</span>
        <span>public</span><span> function setConfigs(array $configs){
            </span><span>if</span>(count($configs) > <span>0</span><span>){
                </span><span>foreach</span> ($configs <span>as</span> $k =><span> $v) {
                    </span><span>if</span>(isset($<span>this</span>-><span>$k))
                        $</span><span>this</span>->$k =<span> $v;
                }
                </span><span>return</span> <span>true</span><span>;
            }
            </span><span>return</span> <span>false</span><span>;
        }</span>

 

 

测试

模板文件 testTpl.html

<!DOCTYPE html>
<html lang=<span>"</span><span>en</span><span>"</span>>
<head>
    <meta charset=<span>"</span><span>UTF-8</span><span>"</span>>
    <title>test_tpl_demo</title>
</head>
<body><span>
    {$name}:{$age}:{$message}
</span></body>
</html>

运行文件 test_tpl.php

<?<span>php
    require </span><span>'</span><span>Tpl.class.php</span><span>'</span><span>;
    
    $tpl </span>= <span>new</span><span> Tpl();
    $tplarr </span>=<span> array(
            </span><span>'</span><span>name</span><span>'</span>=><span>'</span><span>waited</span><span>'</span><span>,
            </span><span>'</span><span>age</span><span>'</span>=><span>'</span><span>100</span><span>'</span><span>
        );
    $tpl</span>-><span>assign($tplarr);
    $tpl</span>->assign(<span>'</span><span>message</span><span>'</span>,<span>'</span><span>this is a demo</span><span>'</span><span>);
    $tpl</span>->display(<span>'</span><span>testTpl.html</span><span>'</span><span>);

</span>?>

输出:waited:100:this is a demo

生成编译文件:972fa4d270e295005c36c1dbc7e6a56c.php

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1107665.htmlTechArticlephp模板引擎技术简单实现,php模板引擎 用了smarty,tp过后,也想了解了解其模板技术是怎么实现,于是写一个简单的模板类,大致就是读取模...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn