首页 > 作文

Smarty模板类内部原理实例分析

更新时间:2023-04-07 15:04:00 阅读: 评论:0

本文实例讲述了smarty模板类内部原理。分享给大家供大家参考,具体如下:

之前在学习thinkphp的时候,有接触到smarty模板类,但是一直不知道其内部实现的原理,博主今天终于知道了其内部原理,其实也挺简单的,然后写了一个迷你版的smarty模板类,对理解其内部原理有了很大的帮助。

1、迷你版smarty类

首先上代码,最后再进行讲解。

项目结构图

minismarty类代码(minismarty.class.php)

<?php/** * 迷你模板类 */class minismarty{  public $template_dir = '';//模板文件放置的目录  public $compile_dir = '';//编译后文件放置的目录  public $tpl_var = array();//模板赋值的变量  /**   * 给模板进行赋值   * @param str $key  键   * @param mixed $value 值   * @return void   */  public function assign($key,$value){    $this->tpl_var[$key] = $value;  }  /**   * 编译模板,并引入编译后的文件   * @param str $template 模板文件   * @return void   */  public function display($template){    $compile_file = $this->compile($template);    include($compile_file);  }  /**   * 将模板文件编译成php文件   * @param str $template 模板文件名   * @return str      编译文件名   */  private function compile($template){    $template_file = $this->template_dir.'/'.$template;    //读取模板文件中的内容    $source = file_get_contents($template_file);    //判断是否需要再次生产编译文件    $compile_file = $this->compile_dir.'/'.$template.'.php';    //如果存在编译文件且编译文件的修改时间比模板文件大,则不用再次编译,直接返回文件路径    if(file_exists($compile_file) && filemtime($compile_file) > filemtime($template_file)){      return $compile_file;    }    //解析{$}为<?php echo 等操作    $source = str_replace('{$', '<?php echo $this->tpl_var[\'', $source);    $source = str_replace('}', '\'];?>挑衅造句;', $source);    //生成编译文件    file_put_contents($compile_file, $source);    //返回编译后的文件路径    return $compile_file;  }}?>

测试模板类代码(te考普通话stsmarty.php)

<?php//1、引入并创建模板实例include ('./minismarty.class.php');$smarty = new minismarty();$smarty->template_dir = './template';$smarty->compile_dir = './compile';//2、给模板对象赋值$title = '两会召开';$content = '好奶粉,好会议,好新闻';$smarty->assign('title',$title);$smarty->assign('content',$content);//3、显示模板$template = 'template.html';$smarty->display($template);?>

模板文件(template.html)

<!doctype html><html><head>  <meta chart="utf-8">  <meta http-equiv="x-ua-compatible" content="ie=edge">  <title>{$title}</title>  <link rel="stylesheet" href=""></head><body>  <h3>{$content}</h3></body></html>

编译后的文件(template.html.php)

<!doctype html><html><head>  <meta chart="utf-8">  <meta http-equiv="x-ua-compatible" content="ie=edge">  <title><?php echo $this->tpl_var['title'];?></title>  <link rel="stylesheet" href=""></head><body>  <h3><?php echo $this->tpl_var['content'];?></h3></body></html>

代码都贴完了,最后解释一下。在测试模板类(testsmarty.php)文件中,首先是引入模板类文件,实例化模板对象,然后给模板对象赋值,最后显示模板。在模板类(minismarty.class.php)文件中,有3个属性和3个方法,属性分别是template_dircompile_dir‘和tpl_var,含义分别是模板文件的路径、编译后文件的路径、模板对象的变量。3个方法分别是assigndisplaycompile,assign方法是给模板对象赋值,display方法是编译模板文件,并引入(显示)编译后的文件,compile方法是编译模板文件。编译模板文件的过程主要是将模板文件中的{$标签}解析成<?php echo $var?> 等php代码。

2、smarty原理分析

工作流程

(1)六年级上册语文补充习题答案把需要显示的全局变量,赋值,塞到对象的内部属性中的一个数组里
(2)然后编译模板,将{$标签}解析成相应的php乳腺癌化疗期间饮食 echo 代码
(3)引入编译后的php文件

使用步骤

(1)smarty是一个类,要使用的话,必须引入在进行实例化
(2)使用assign给模板赋值
(3)使用display方法【从编译到输出】

smarty的缺点

(1)编译模板,浪教资多少分算过费时间
(2)要把变量再重新赋值到对象的属性中,增大了开销

本文发布于:2023-04-07 15:03:59,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/8e7a95d09a0af5a628d9048b48c3539c.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:Smarty模板类内部原理实例分析.doc

本文 PDF 下载地址:Smarty模板类内部原理实例分析.pdf

标签:模板   文件   赋值   对象
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图