Heim  >  Artikel  >  Backend-Entwicklung  >  Was ist das Prinzip der PHP-Template-Engine?

Was ist das Prinzip der PHP-Template-Engine?

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-08-27 10:22:382911Durchsuche

Was ist das Prinzip der PHP-Template-Engine?

PHP的模板引擎smarty原理

mvc是开发中的一个伟大的思想,使得开发代码有了更加清晰的层次,让代码分为了三层各施其职、无论是对代码的编写以及后期的阅读和维护,都提供了很大的便利。

我们在php开发中,视图层view是不允许有php代码来操作数据库之类的来获取数据的,我们一般都会在控制器层controller,就已经把视图层要展示的数据准备好,方便视图层直接用来展示。

smarty模板技术,可以让数据和视图进行分离,让视图中不能直接出现php代码。这样的话,让前段页面的开发和后台数据的开发,可以双管齐下,同时进行了。

smarty模板的使用比较简单,主要有两个非常核心的函数。一个是assig(),把模板中要使用的数据进行赋值,一个是display(),用来解析和展示最后的视图模板。

相关推荐:《PHP教程

使用的简单代码如下:

include_once "Smarty.class.php";//引入smarty类
$smarty = new Smarty;//创建smarty对象
$smarty->assign("name","zhangmiao");//赋值,以备模板中使用
$smarty->assign("age","18");
$smarty->display('index.tpl');//引入模板,展示视图页面

我们看下模板源代码是这样的:

<h1>测试模板1</h1>
我的名字是:{$name}<br/>
我的年纪是:{$age}<br/>

浏览器页面是这样的:

Was ist das Prinzip der PHP-Template-Engine?

疑问:我们模板中没有php代码,我们只用了{$name}和{$age}就能把对应的变量给展示了出来,是什么鬼呢?

然后,我们一看smarty编译后的文件是这样的:

<h1>测试模板1</h1>
我的名字是:<?php echo $this->var["name"]; ?><br/>
我的年纪是:<?php echo $this->var["age"]; ?><br/>

原来如此,最终还是变成了含有php代码的模板,但是这个模板中把标签转成php代码的工作,我们交给了smarty模板引擎来完成的。

那到底smarty模板引擎是怎么把模板里面的非php代码的标签,转变成被最终可以解析执行的php代码的呢?

主要思路是:替换。

分成了两步:

1、通过assign函数把要解析的标签变量赋值

2、通过display函数把标签替换成对象的php变量

我们根据这个思路,自己也写了个简易版的smarty模板引擎,算是多smarty模板引擎设计原理的一种理解。但是只能解析单个变量的标签,其他标签均没有处理。代码如下:

class MySmarty{
    //模板存放路径
    public $template = &#39;./template/&#39;;
    //编译后模板路径
    public $template_c = &#39;./template_c/&#39;;
    //存放变量的数组,通过assign函数赋值
    public $var = array();
    //变量赋值
    public function assign($vkey,$value){
        if($vkey != ""){
            $this->var[$vkey] = $value;//把要模板中解析的数据压入数组
        }
    }
    //模板中变量替换
    public function display($path){
        $template_path = $this->template.$path;
        $template_c_path = $this->template_c.$path.".php";
        if(!file_exists($template_path)){
            return false;
        }
        //只有解析后的模板文件不存在或者模板源文件有新的改动的情况下 重新解析模板       
        if(!file_exists($template_c_path) || filemtime($template_path)>filemtime($template_c_path)){
            //获取模板源文件,用来替换
            $template_content = file_get_contents($template_path);
            $pattern = array(
                &#39;/\{\s*\$([a-zA-Z][a-zA-Z0-9_]*)\s*\}/i&#39;
            );
            $replace = array(
                &#39;<?php echo $this->var["${1}"]; ?>&#39;
            );
            //用正则去替换模板源文件中的变量符号{$varname}
            $res = preg_replace($pattern,$replace,$template_content);
            //编译后文件写入某个目录
            file_put_contents($template_c_path,$res);
        }
        //引入编译后文件,其实就是执行了那个php文件的代码
        include_once $template_c_path;
    }
}

我们调用自己的assign和display放入引入,也能正常解析使用啦。

Das obige ist der detaillierte Inhalt vonWas ist das Prinzip der PHP-Template-Engine?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn