php学习之Smarty——Smarty3

14 篇文章 0 订阅
10 篇文章 0 订阅

可参考资源:http://www.welefen.com/tag/smarty3

一.概述 

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=foovalue=['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.  变量名里可以包含变量或者表达式,这个强大。如:

$foonormal 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@iterationnumber of iteration
$x@totaltotal number of iterations
$x@firsttrue on first iteration
$x@lasttrue on last iteration

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

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

$var@keyforeach $var array key
$var@iterationforeach current iteration count (1,2,3…)
$var@indexforeach current index count (0,1,2…)
$var@totalforeach $var array total
$var@firsttrue on first iteration
$var@lasttrue on last iteration

12. 新增了while标签,如:

{while $foo}…{/while}

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

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

{time()}

{function name=menu level=0}

<ulclass=”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:

1<html>
2<head>
3<title>{block name="title"}My site name{/block}</title>
4</head>
5<body>
6<h1>{block name="page-title"}Default page title{/block}</h1>
7<divid="content">
8{block name="content"}Default content{/block}
9</div>
10</body>
11</html>

child.tpl:

1{extends file="parent.tpl"}
2{block name="title"}
3Child title
4{/block}

grandchild.tpl:

1{extends file="child.tpl"}
2{block name="title"}Home-{$smarty.block.parent}{/block}
3{block name="page-title"}My home{/block}
4{block name="content"}
5{foreach $images as $img}
6<imgsrc="{$img.url}"alt="{$img.description}"/>
7{/foreach}
8{/block}

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

1<html>
2<head>
3<title>Home-Child title</title>
4</head>
5<body>
6<h1>My home</h1>
7<divid="content">
8<imgsrc="/example.jpg"alt="image"/>
9<imgsrc="/example2.jpg"alt="image"/>
10<imgsrc="/example3.jpg"alt="image"/>
11</div>
12</body>
13</html>

几点需要注意的:

1.  子模板里只能有{extends}和{block},其他标签会被忽略;【待验证,反正我加入了Include标签】

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

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

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

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

{blockname=’title’ append} My title {/block}

四.其他

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

五.总结

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值