Smarty模板引擎技术(三)

一、Smarty模版引擎的内置函数(或标记)

  foreach标记
    1.foreach标记处理数组
      在模版中必须使用成对的foreach标签遍历数组中的数据,
      而且必须设置from和item两个属性
      格式:{foreach from=$users item=user key=key}
    2.foreach标记的属性
      (1)from:待循环数组的名称
      (2)item:当前元素的变量名称
      (3)key:当前元素的键名
      (4)name:该循环的名称
    3.foreachelse子标记
      (1)当from属性指定的数组为空时,则输出foreachelse区域中的内容
      (2)foreachelse必须与foreach一起使用
      (3)foreachelse没有结束标记
    4.使用案例:
    //循环一维数组:aaa为数组中当前元素的变量名称
      {foreach from=$user item=aaa}
        {$aaa}
      {/foreach}

    //循环二维数组:如果users代表多个用户,那么user就代表当前用户
      {foreach from=$users item=user}
        姓名:{$user.username}
        性别:{$user.sex}
      {/foreach}

    //foreachelse子标记的用法:
      {foreach from=$users item=user}
        姓名:{$user.username}
        性别:{$user.sex}
      {foreachelse}
        暂时没有数据
      {/foreach}

  include标记
    1.在模版文件中包含子模版
      格式:{include file=“模版文件路径”}
    2.例如
      (1)包含公共头部文件: {include file=“header.html”}
      (2)包含公共尾部文件: {include file=“footer.html”}
    3.两个比较使用的特性:
      (1)可在include标记中传入可选的assign属性,将导入的子模版内容不再当前模版中输出,而是赋值给assign属性指定的变量。
        例如:{include file=“header.html” assign=“header”}
      (2)可以在引入子模版的同时向其传递各种属性
        例如:{include file=“header.html” title=“头部文件”}

  if标记
    1.常跟条件修饰符一块使用
    2.常见的条件修饰符
      (1) eq:相等 neq:不等
      (2) mod:求模 gt:大于
      (3) is even:是否是偶数 not:非
      (4) ge:大于等于 lt:小于
      (5) lte:小于等于
      (6) ==:相等 !=:不相等
      (7) >:大于 <:小于
      (8)<=:小于等于 >=:大于等于
    3.使用
      (1)模版文件中使用smarty模版引擎的if条件语句
        {if $name eq ‘admin’}
          <font color=“red”>管理员登录</font>
        {else}
          <font color=“red”>你没有登录权限</font>
        {/if}
      (2)模版文件中使用smarty模版引擎的if条件语句
        {if $quanxian eq ‘admin’}
          <font color=“red”>管理员登录</font>
        {elseif $name eq ‘xiaosan’}
          <font color=“red”>普通会员登录</font>
        {else}
          <font color=“red”>你没有登录权限</font>
        {/if}

  literal标记
    1. literal文本的处理技巧
      literal中的数据将被当作文本处理,此时模版将忽略其内部的所有字符信息该特性用于显示有可能包含大括号等字符信息的javascript脚本
    2. 例如:literal标记中的所以字符信息将被当作文本来处理
      {literal}<script>…</script>{/literal}

  section标记
    1. section标记处理数组
      在模版中必须使用成对的section标签遍历数组中的数据,而且必须设置name和loop两个属性
      格式:{section name=i step=2 start=2 max=10 loop=$users}
    2. section标记的属性:
      (1)name:指定该循环的名称,当需要在section循环内输出变量时,必须在变量后加上中括号,中括号中必须包含name变量
      (2)loop:循环变量的名称
      (3)start:循环执行的初始索引位置
      (4)step:该值决定循环的步长,例如:step=2将之遍历下标为0、2、4等元素
      (5)max:设定最大循环次数,比如:max=2,代表最多循环2次
      (6)show:决定是否显示该循环,默认值为1-显示循环 0-禁止循环
    3. section循环区域中可以调用的变量
      在section循环中有一些可供调用的变量名,用来访问该循环中一些特殊的值,而且在循环中必须通过smarty保留变量$smarty进行访问
      格式:{$smarty.foreach.foreachname.varname}
      (1)first:当前section循环在第一次执行时该变量的值为true
      (2)last:当前section循环在最后一次执行时该变量的值为true
      (3)rownum:循环的行数,与iteration变量差不多
      (4)index:显示当前循环的索引
      (5)iteration:显示循环的次数
    4. sectionelse字标记
      (1)当loop属性指定的数组为空时,则输出sectionelse区域中的内容
      (2)sectionelse必须与section一起使用
      (3)sectionelse没有结束标记

      格式:
      {section name=i loop=$users}
        {$users[i].username}
      {sectionelse}
        <div><font color=“red”>暂时没有数据</font></div>
      {/section}

二、Smarty缓存的应用

  设置缓存
    (1)$smarty->caching=true; //开启缓存
    (2)$smarty->cache_dir=“./cache”;//设置缓存目录
    (3)$smarty->cache_lifetime=7*24*60*60;//设置缓存时间
    (4)$smarty->display(“index.html”);

  每个页面多个缓存
    比如:发布文章的时候,使用同一模版时会生成不同的页面显示。如果开启缓存,则通过同一个模版生成的多篇文章(多个实例)都需要被缓存,smarty实现这个比较容易,只需要在调用display()方法时,通过在第二个可选参数中提供一个值,这个值是为每一篇文章(或实例)指定的一个唯一的标识符,有几个不同的标识符就有几个不同的缓存。
    比如:$smarty->display(“news.html”,$_GET[‘newid’]);

  怎样判断某个文件被缓存了?
    (1) $smarty->is_cached(“index.html”);
    (2) $smarty->is_cached(“news.html”,$_GET[‘newsid’]);

  缓存的清除
    (1) $smarty->clear_all_cache(); //清除所有缓存
    (2) $smarty->clear_cache(‘index.tpl’); // 清除某一模版的缓存
    (3) $smarty->clear_cache(‘index.tpl’,cache_id); // 清除指定缓存号的缓存

  局部缓存
    1. insert函数默认是不缓存的,并且这个属性不能修改
    2. 模版文件中:
      <div>{insert name=“get_time”}</div>
    3. php文件中:
      function insert_get_time(){
        return date(“Y-m-d H:i:s”);
      }
    4. 注意:方法的前缀必须是insert

 三、模板继承

  继承带来了模板面向对象概念(oop),它允许你定义一个或多个基模板供子模板继承。继承意味着子模板可覆盖所有或部份父模板中命名相同的块区域。 

  子模板不能定义任何内容,除了需要覆盖父模板的{block}标签块,所有在{block}标签外的内容将被自动移除。
  子模板和父模板的{block}标签内容可以合并:

    方法一:用append添加或prepend追加{block}标签选项标记;

    方法二:使用{$smarty.block.parent}或{$smarty.block.child}占位符。
  模板继承是一种编译时进程,其将建立一个独立的编译模板文件。与对应的基于载入{include}子模板解决方案相比,当解释模板时,前者有更好的性能。
  子模板通过{extends}标签定义继承父模板,该定义应写在子模板的第一行。与在模板文件中使用{extends}标签不同的是,你可以使用“extends:模板资源类型”(见extends:resource),调用fetch()或display()函数在php脚本中定义整个模板继承树。后一个方法提供更大的弹性。

实例:

 

 1 laout.html        父模板
 2 
 3 <!DOCTYPE html>
 4 <html lang="en">
 5 <head>
 6     <meta charset="UTF-8">
 7     <title>{block name=title}Default Page Title{/block}</title>
 8     <span style="color:blue;">{block name=head}{/block}</span>
 9 </head>
10 <body>
11     {block name=body}{/block}
12 </body>
13 </html>
14 
15 myproject.html       子模板
16 
17 {extends file="./layout.html"}
18 {block name=head}
19     <link rel="stylesheet" href="/css/mypage.css" type="text/css" />
20     <script src="/js/mypage.js"></script>
21 {/block}
22 
23 mypage.html      孙子模板
24 
25 {extends file="./myproject.html"}
26 {block name=title}My Page Title{/block}
27 {block name=head}
28     <link href="/css/mypage.css" rel="stylesheet" type="text/css"/>
29     <script src="/js/mypage.js"></script>
30 {/block}
31 {block name=body}My HTML Page Body goes here{/block}
32 
33 渲染:
34 $smarty->display("mypage.html");
35 
36 输出结果:
37 
38 <!DOCTYPE html>
39 <html lang="en">
40 <head>
41     <meta charset="UTF-8">
42     <title>My Page Title</title>
43     <span style="color:blue;">
44     <link href="/css/mypage.css" rel="stylesheet" type="text/css"/>
45     <script src="/js/mypage.js"></script>
46 </span>
47 </head>
48 <body>
49     My HTML Page Body goes here
50 </body>
51 </html>
原文地址:https://www.cnblogs.com/yexiang520/p/5751112.html