zoukankan      html  css  js  c++  java
  • php学习之Smarty----调用变量 转

    php学习之Smarty------Smarty3  

    2010-06-29 16:47:05|  分类: php+ajax |举报 |字号 订阅

     
    一.概述

    Smarty3目前最新版本是RC1,尚未稳定。

    Smarty3基本保持了向前兼容,但有下面几点改变:

    1.   Smarty3需要php5,不再兼容php4;

    2.   Smarty3默认禁止了{php}标签,如果要启用需要设置一下:$smarty->allow_php_tag = true;

    3.   现在,定界符内两端不能有空白,如:{ foreach }不会匹配foreach标签,而是直接输出“{ foreach  }”,这样{literal}这个标签就不需要了;

    4.   Smarty3 Api有很多改变,虽然Smarty2的Api仍然可以用,但已经不被推荐。
    二.Smarty3模板引擎的新功能

    1.   任何地方都可以用表达式了,而且表达式里可以直接用php里的函数了。这样,之前的math函数基本就用不上。如:

    {$x+$y}

    will output the sum of x and y

    {$foo = strlen($bar)}

    function in assignment

    {assign var=foo value= $x+$y}

    in attributes

    {$foo = myfunct( ($x+$y)*3 )}

    as function parameter

    {$foo[$x+3]}

    as array index

    2.   一个标签里可以使用另外的标签。如:{$foo = ${counter} + 3}

    3.   在双引号里也可以使用标签。如:{$foo = "this is message {counter}"}

    4.   直接在模板里定义数据。如:

    {assign var=foo value=[1,2,3]}

    {assign var=foo value=['y'=>'yellow','b'=>'blue']}

    {assign var=foo value=[1,[9,8],3]} {*数组可以多层*}

    5.   assign变量更容易了,不需要用assign标签,如:{$foo = $bar + 2}

    6.   下面这种写法也是支持的,赋值前会先自动根据需求转为数组:

    {$foo['bar'] = 1}

    {$foo['bar']['blar'] = 2}

    {$foo = 1}

    {$foo[] = 1}

    7.   下面左右两种写法是等价的:

    {$foo.a.b.c}

    $foo['a']['b']['c']

    {$foo.a.$b.c}

    $foo['a'][$b]['c']  

    {$foo.a.{$b+4}.c}

    $foo['a'][$b+4]['c']

    {$foo.a.{$b.c}}

    $foo['a'][$b['c']]  

    8.   变量名里可以包含变量或者表达式,这个强大。如:

    $foo

    normal variable

    $foo_{$bar}

    variable name containing other variable

    $foo_{$x+$y}

    variable name containing expressions

    $foo_{$bar}_buh_{$blar}

    variable name with multiple segments

    {$foo_{$x}}

    will output the variable $foo_1 if $x has a value of 1.


    9.   支持对象的链式调用,如:{$object->method1($x)->method2($y)}

    10.  新增了for这个标签,用来代替section(但是section仍然被支持),如:

    {for $x=0, $y=count($foo); $x<$y; $x++}  ....  {/for}

    特别的,for还支持下面这种用法:

    {for $x = $start to $end step $step} ... {/for}   {*注:貌似正式版才支持step*}

    {for $x = $start to $end} ... {/for}

    而且,在循环中,提供了以下变量:

    $x@iteration

    number of iteration

    $x@total

    total number of iterations

    $x@first

    true on first iteration

    $x@last

    true on last iteration


    11.  foreach也有改进(之前的foreach写法继续被支持),如:{foreach $myarray as $var}...{/foreach}

    在foreach循环中,提供了以下变量:

    $var@key

    foreach $var array key

    $var@iteration

    foreach current iteration count (1,2,3...)

    $var@index

    foreach current index count (0,1,2...)

    $var@total

    foreach $var array total

    $var@first

    true on first iteration

    $var@last

    true on last iteration

    12.  新增了while标签,如:

    {while $foo}...{/while}

    {while $x lt 10}...{/while}

    13.  支持直接使用PHP里的函数(所以{php}{/php}就显得不是那么重要了),如模板中可以支持这么写:

    {time()}

    ---

    {function name=menu level=0}

      <ul class="level{$level}">

      {foreach $data as $entry}

        {if is_array($entry)}

          <li>{$entry@key}</li>

           {menu data=$entry level=$level+1}

        {else}

          <li>{$entry}</li>

        {/if}

      {/foreach}

      </ul>

    {/function}

    ---

    {$menu = ['item1','item2','item3' => ['item3-1','item3-2','item3-3' =>  ['item3-3-1','item3-3-2']],'item4']}

    {menu data=$menu}

    输出:

        * item1

        * item2

        * item3

              o item3-1

              o item3-2

              o item3-3

                    + item3-3-1

                    + item3-3-2

        * item4

    三.模板继承

    现在在父模板里可以定义block,在子模板里可以使用或修改block,如:

    parent.tpl:

    <html>
    <head>
    <title>{block name="title"}My site name{/block}</title>
    </head>
    <body>
    <h1>{block name="page-title"}Default page title{/block}</h1>
    <div id="content">
    {block name="content"}Default content{/block}
    </div>
    </body>
    </html>

    child.tpl:

    {extends file="parent.tpl"}
    {block name="title"}
    Child title
    {/block}

    grandchild.tpl:

    {extends file="child.tpl"}
    {block name="title"}Home-{$smarty.block.parent}{/block}
    {block name="page-title"}My home{/block}
    {block name="content"}
    {foreach $images as $img}
    <img src="{$img.url}" alt="{$img.description}"/>
    {/foreach}
    {/block}

    grandchild.tpl渲染出来是这个样子:

    <html>
    <head>
    <title>Home-Child title</title>
    </head>
    <body>
    <h1>My home</h1>
    <div id="content">
    <img src="/example.jpg" alt="image"/>
    <img src="/example2.jpg" alt="image"/>
    <img src="/example3.jpg" alt="image"/>
    </div>
    </body>
    </html>

    几点需要注意的:

    1.   子模板里只能有{extends}和{block},其他标签会被忽略;

    2.   Smarty3支持无限级继承,但显然更深层次的继承意味着更多的开销;

    3.   除了可以在子模板里支持extends的父模板,也可以在php里这么写:

    $smarty->display('extends:parent.tpl|child.tpl|grandchild.tpl');

    4.   block标签里还支持append、prepended两个选项,表示追加和前置追加,如:

    {block name='title' append} My title {/block}

    四.其他

    Smarty3里还支持其他许多新特性,这里先就不介绍了。如Plugin、PHP  Templates、Varialbe Scope和Variable Storage。

    五.总结

    Smarty3做了很多易用性升级,新增的一些语法可以大大提供开发效率;新增的模板继承也有利于我们更好的规划模板结构,性能方面据官方说明提高了200%~300%。因为现在还是RC版,稳定性和性能方面有待进一步观察。建议我们保持密切关注,合适的时候再升上去。

  • 相关阅读:
    Java MyBatis 插入数据库返回主键
    FISCO-BCOS平台共识
    分布式一致性协议介绍(Paxos、Raft)
    分布式问题分析
    分布式基础知识
    比特币编译(Ubuntu 16.04)
    比特币源代码分析(1)
    c++中的多线程
    剑指offer中数据结构与算法部分学习
    基础的语法知识汇总
  • 原文地址:https://www.cnblogs.com/zhuanli/p/3835223.html
Copyright © 2011-2022 走看看