smarty入门教程三-------Smarty引擎的工作原理

1. Smarty的工作原理是:  Smarty自带编译类,作用是将模版中的标签替换成PHP代码,每次检查PHP源码的修改时间,只有PHP修改了,才重新编译,所以Smarty的性能还可以。下面我们自己编写一个简单的编译类,实现替换模板中标签的功能,实现一个最小功能的Smarty模板,一次来理解Smarty的运行原理。

首先不使用模板时,来看一个最简单的用PHP输出的例子:

<?php
$title="this is the title";
$content="smarty yuanli is this";
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><?php echo $title?></title>
</head>
<body>
<p>内容:<?php echo $content?></p>
</body>
</html>

HTML代码负责表示层,PHP代码负责取出需要的数据输出,这里是title和content。可以看到,这是PHP代码和HTML代码是混合在一起的。模板的目的就是从中分离出PHP代码,换成其他语法更简单的,更接近HTML语法的标签语言实现与嵌入PHP相同的功能。

为什么不直接使用PHP而要再引入一种新的语法的呢?

一是为了让专注于页面设计的人员不必学习PHP而只学习Smarty语法就能完成设计,Smarty语法与PHP相比简单很多也容易学习,更接近页面设计人员掌握的HTML,CSS等语法

二是如果在页面中也使用PHP,编程人员很难保证不会把其他不属于显示的PHP代码放在HTML页面中。

简单来看,实现一个模板引擎,只需要编写一个类,实现将HTML中新定义的模板语法翻译成PHP代码即可,实际上就是用正则表达式匹配替换字符串的过程。

smarty.class.php(简单实现):

<?php
    class Smarty{
        public $template_dir;//模板目录
        public $compile_dir;//编译目录
        public $arr=array();//定义一个数组,用以存放assign中的第二个参数传过来的值
        public function __construct($template_dir="../templates",$compile_dir="../templates_c"){
                $this->template_dir=$template_dir;//模板目录
                $this->compile_dir=$compile_dir;  //编译目录
            }
        public function assign($content,$replacment=null){
                if($content!=""){                 //如果指定模板变量,才将要赋的值存储到数组中
                        $this->arr[$content]=$replacment;
                    }
            }
        public function display($page){
                $tplFile=$this->template_dir."/".$page;//读取模板文件,注意:如果模板目录下还有子目录,记得要写完整,比如,$smarty->display('Default/index.tpl')
                if(!file_exists($tplFile)){
                        return;
                }
                $comFile=$this->compile_dir."/"."com_".$page.".php";
                $tplContent=$this->con_replace(file_get_contents($tplFile));//将smarty标签替换为php的标签
                file_put_contents($comFile,$tplContent);
                include $comFile;
        }
        public function con_replace($content){
                $pattern=array(
                    '/<{\s*\$([a-zA-Z_][a-zA-Z_0-9]*)\s*}>/i'
                );
                   $replacement=array(
                       '<?php echo $this->arr["${1}"] ?>'
                );
                    return preg_replace($pattern,$replacement,$content);
                }
        }
?>

smarty.ini.php: 用来配置Smarty,这里和真正的Smarty使用前的配置差不多,只是比较简单。

<?php
    include "Smarty.class.php";
    $tpl=new Smarty();
    $tpl->template_dir="./templates";    
    $tpl->compile_dir="./compile";
?>

模板文件index.html:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><{$title}></title>
</head>
<body>
<p>内容:<{$content}></p>
</body>
</html>

index.php :

<?php
    include "./smarty.ini.php";
    $title="this is the title ";    
    $content="this is the content ";
    $tpl->assign("title",$title);
    $tpl->assign("content",$content);    
    $tpl->display("index.html");
?>

此时网站的目录结构是这样的:

Smarty-test---------compile-------com_index.html.php(经过smarty.class.php编译后生成的文件)

------------------------template------index.html

------------------------index.php

-----------------------smarty.class.php

-----------------------smarty.ini.php

好的,我们来看看编译后的smarty.class.php文件:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><?php echo $this->arr["title"] ?></title>
</head>
<body>
<p>内容:<?php echo $this->arr["content"] ?></p>
</body>
</html>

是不是和没有使用模板前PHP与HTML混合时的时候相同。

这就是一个非常简单的模版引擎了。Smarty的基本原理就是这样,只不过在此基础上多了很多方便使用的语法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值