代码解释类似smart模版引擎原理

php中文网
发布: 2016-07-29 09:05:00
原创
1433人浏览过

先贴下目录结构:

<code> ls
compiled/  index.php  source/  ss.class.PHP

compiled 模版编辑后的目录
index.php
source 模版源文件夹
ss.clsss.php 山寨类smarty文件夹
</code>
登录后复制
<code>ss.clsss.php
</code>
登录后复制
<code><?php
    class Template{

        private $templateDir;//源文件文件夹
        private $compileDir;//编译之后文件夹
        private $leftTag="{#";//需要替换的左边标记
        private $rightTag="#}";//需要替换的左边标记
        private $templateExtName=".html";
        private $currentTemp;//当前正在编译的模版文件名
        private $outputHtml;//正在读取的模版文件
        private $varPool=array();//模版变量池

        public function__construct($templateDir,$compileDir,$leftTag=null,$rightTag=null,$templateExtName=null){

            $this->templateDir=$templateDir;
            $this->compileDir=$compileDir;

            if(!empty($leftTag))$this->leftTag=$leftTag;
            if(!empty($rightTag))$this->rightTag=$rightTag;
            if(!empty($templateExtName))$this->templateExtName=$templateExtName;
        }

        // 把用到的变量放入到变量池子中
        public function assign($tag,$var){
            $this->varPool[$tag]=$var;
        }

        // 取出一条变量,用于下面的str_replace正则替换
        public function getVar($tag){
            return $this->varPool[$tag];
        }

        //获取模版源文件
        public function getSourceTemplate($templateName){

            $this->currentTemp=$templateName;

            // 拼接完整文件名
            $sourceFileName=$this->templateDir.$this->currentTemp.$this->templateExtName;
            $this->outputHtml=file_get_contents($sourceFileName);
        }

        // 编译
        public function compileTemplate($templateName=null){

            // 获取当前需要编译的模版
            $templateName=empty($templateName)?$this->currentTemp:$templateName;

            // $pattern='/{#($[a-zA-Z_]w+)#}/';// 符合php变量命名

            //preg_quote 可对用户界定的左右定界符中出现.  + * ? [ ^ ] $ ( ) { } = ! < > | : -进行转义
            $pattern="/".preg_quote($this->leftTag);

            //前后加上空格匹配任意次匹配类似<h1>{# $name  #}</h1>
            $pattern.=' *$([a-zA-Z_]w*) *';

            $pattern.=preg_quote($this->rightTag)."/";

            $this->outputHtml=preg_replace($pattern, '<?php echo $this->getVar('$1');?>', $this->outputHtml);// $1写成\1也可以 

            // 注意不要用双引号,会解析getvar Template::$getVar报notice错误
            $this->outputHtml=preg_replace($pattern, "<?php echo $this->getVar(\1);?>", $this->outputHtml);

            $compileFileName=$this->compileDir.md5($templateName).$this->templateExtName;

            file_put_contents($compileFileName, $this->outputHtml);
        }

        // 输出
        public function display($templateName=null){

            $templateName=empty($templateName)?$this->currentTemp:$templateName;

            include($this->compileDir.md5($templateName).$this->templateExtName);

        }

    }</code>
登录后复制

简单调用如下:(index.php中内容)

Smart Picture
Smart Picture

Smart Picture 智能高效的图片处理工具

Smart Picture 40
查看详情 Smart Picture
<code><?php
    include('ss.class.php');

    $basedir=str_replace('\', '/', dirname(__FILE__));//兼容linux等区分大小写平台

    $test=new Template($basedir."/source/",$basedir."/compiled/");

    $test->assign('name','iamtb');

    $test->assign('pageTitle','tbtbt');

    $test->getSourceTemplate('index');

    $test->compileTemplate('index');

    $test->display('index');
</code>
登录后复制

以上就介绍了代码解释类似smart模版引擎原理,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号