zoukankan      html  css  js  c++  java
  • smarty课程---最最最简单的smarty例子

    smarty课程---最最最简单的smarty例子

    一、总结

    一句话总结:其实所有的模板引擎的工作原理是差不多的,无非就是在php程序里面用正则匹配将模板里面的标签替换为php代码从而将两者混合为一个php的混编文件,然后执行这个混编文件。

    smarty的两个主要函数:

    assign->分配变量
    display->加载模板

    1、smarty的功能是什么?

    用一个php文件给一个html文件分配变量
    其实也是模板和控制器分离(也就是mvc模式)

    2、smarty的两个函数的主要作用是什么?

    assign->分配变量
    display->加载模板
    替换模板中的变量,例如把{$name}替换为<? echo $this->arr['name'];?>
    然后用include加载执行这个模板

    3、我们在外部访问的是哪个文件?

    访问的是index.php,而不是index.html,也就是相对于thinkphp里面的控制器,我们根本就没有访问模板,模板只是作为模板文件使用,编译好后被扔到了控制器里面
    也就是说,在thinkphp里面我们只访问了控制器,而模板里面的内容是扔到了控制器里面,我们根本没有访问模板,我们一直都只是在控制器

    4、display函数里面为什么不能用echo而用include?

    直接echo的话php代码不执行,因为echo本身就在php里面,所以不能接着套php标签,而编译好的模板里面是php代码

    include作用:不仅仅是引入,还执行

     9     function display($file){
    10         $str=file_get_contents($file);
    11         $ptn='/{$(.+)}/i';
    12         $rep='<?php echo $this->arr["$1"];?>';
    13         $rst=preg_replace($ptn, $rep, $str);
    14         $dstfile="templates_c/".md5($file).".php";
    15         file_put_contents($dstfile, $rst);
    16         include($dstfile);
    17         //echo "$str";
    18         //直接echo的话php代码不执行,因为echo本身就在php里面,所以不能接着套php标签
    19     }

    二、最最最简单的smarty例子

    1、截图

    目录结构

    运行成功后的样例

     

    2、代码

    index.php
    1 <?php
    2 include("libs/Smarty.class.php");
    3 
    4 $s=new Smarty();
    5 $s->assign("name","user1");
    6 $s->assign("age","30");
    7 
    8 $s->display("templates/index.html");
    9 ?>
    模板 index.html
     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Document</title>
     6 </head>
     7 <body>
     8     <h1>{$name}</h1>
     9     <h1>{$age}</h1>
    10 </body>
    11 </html>
    Smarty.class.php
     1 <?php
     2 class Smarty{
     3     public $arr;
     4 
     5     function assign($key,$val){
     6         $this->arr[$key]=$val;
     7     }
     8 
     9     function display($file){
    10         $str=file_get_contents($file);
    11         $ptn='/{$(.+)}/i';
    12         $rep='<?php echo $this->arr["$1"];?>';
    13         $rst=preg_replace($ptn, $rep, $str);
    14         $dstfile="templates_c/".md5($file).".php";
    15         file_put_contents($dstfile, $rst);
    16         include($dstfile);
    17         //echo "$str";
    18         //直接echo的话php代码不执行,因为echo本身就在php里面,所以不能接着套php标签
    19     }
    20 }
    21 ?>
    Smarty编译后的:fb5aa1cd1261d08d02db6f7dc314d9ab.php
     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Document</title>
     6 </head>
     7 <body>
     8     <h1><?php echo $this->arr["name"];?></h1>
     9     <h1><?php echo $this->arr["age"];?></h1>
    10 </body>
    11 </html>
     
  • 相关阅读:
    [MacOS]Sublime text3 安装(一)
    [RHEL8]开启BBR
    PAT Advanced 1136 A Delayed Palindrome (20分)
    PAT Advanced 1144 The Missing Number (20分)
    PAT Advanced 1041 Be Unique (20分)
    PAT Advanced 1025 PAT Ranking (25分)
    PAT Advanced 1022 Digital Library (30分)
    PAT Advanced 1019 General Palindromic Number (20分)
    PAT Advanced 1011 World Cup Betting (20分)
    PAT Advanced 1102 Invert a Binary Tree (25分)
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/9585596.html
Copyright © 2011-2022 走看看