温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Smarty模板类内部的原理

发布时间:2021-06-03 18:07:36 来源:亿速云 阅读:149 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关Smarty模板类内部的原理,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

1、迷你版Smarty类

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

项目结构图

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;
  }
}
?>

测试模板类代码(testSmarty.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 charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>{$title}</title>
  <link rel="stylesheet" href="">
</head>
<body>
  <h4>{$content}</h4>
</body>
</html>

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

<!DOCTYPE html>
<html>
<head>
  <meta charset="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>
  <h4><?php echo $this->tpl_var['content'];?></h4>
</body>
</html>

代码都贴完了,最后解释一下。在测试模板类(testSmarty.php)文件中,首先是引入模板类文件,实例化模板对象,然后给模板对象赋值,最后显示模板。在模板类(MiniSmarty.class.php)文件中,有3个属性和3个方法,属性分别是template_dir 、compile_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)要把变量再重新赋值到对象的属性中,增大了开销

上述就是小编为大家分享的Smarty模板类内部的原理了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI