smarty初步框架

html页面

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>模板页面</title>
 </head>
 <body>
 <fieldset style="width:800px;margin:0 auto;">
    <legend>模板页面</legend>
    {$title}
    {$content}
    {$san}
 </fieldset>
 </body>
</html>

php页面

    // 1、引入迷你MiniSmarty.class.php
    require "MiniSmarty.class.php";
    // 2、实例化smarty对象
    $smarty = new MiniSmarty;
    // 3、把字符串信息设置为模板引擎类的属性信息
    $smarty -> assign('title','九尾狐标题');
    $smarty -> assign('content','九尾狐内容');
    $smarty -> assign('san','九尾狐');
    // 4、调用compile 方法,同时传递01.html模板文件参数
    // 5、在该方法里把01.html 内部标记替换为php标记
    $smarty -> display('01.html');

MiniSmarty页面

// 模板引擎类
 class MiniSmarty{

    public $template_dir="./view/";//模板目录
    public $templatec_dir="./view_c/";//编译文件目录


    // 给该类声明属性,用于存储外部的变量信息
    public $tpl_var = array();
    // 把外部变量设置为类内部属性的一部分
    function assign($k,$v){
        $this-> tpl_var[$k] = $v;
    }

    function display($tpl){
        $n = $this->complie($tpl);
        require $n;
    }

    function complie($tpl){
        $con_file = $this->templatec_dir.$tpl.".php";
        $tpl_file = $this->template_dir.$tpl;

        // 1、判断该混编文件必须存在
        // 2、混编文件的修改时间 大于 模板文件的修改时间
        if(file_exists($con_file) && filemtime($con_file) > filemtime($tpl_file)){
            return $con_file;
        }
        echo "firse run new file";
        // 获取模板文件内部具体的模板内容
        $cont = file_get_contents($tpl_file);
        $cont = str_replace("{\$","<?php echo \$this->tpl_var['",$cont);
        $cont = str_replace("}","'] ; ?> ",$cont);
        // 将替换完的内容写入文件中
        file_put_contents($con_file,$cont);
        // 引入文件
        return $con_file;

    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

solly793755670

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值