码迷,mamicode.com
首页 > 其他好文 > 详细

Smarty3——内置函数

时间:2017-03-08 19:16:38      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:_id   append   file   sage   assign   str   code   body   cal   

Table of Content

{$var}

{$append}

{assign}

{block}

{call}

{config_load}

{debug}

{extends}

{for}

{foreach},{foreachelse}

{function}

{if} {elseif} {else}

{include}

{include php}

{insert}

{ldelim} {rdelim}

{literal}

{nocache}

{php}

{section} {sectionelse}

{setfilter}

{strip}

{while}

Smarty自带了一些内置函数,这些函数是模板引擎的组成,会被编译到PHP代码中,以提高性能。在Smarty模板引擎可以进行简单的复制操作,只是复制而不进行输出。

{$var=‘Smary repeat‘}
{*附加到数组中*}
{$num_count[]="add a element to array"}
{*对数组复制*}
{$num_count[0]=‘this is element is changed‘}
{*在包含的模板进行变量复制,其包含的模板内也也可看到*}
{include file=‘var_config.tpl‘}
技术分享

{block}

{block}是在模板上定义一块区域,以进行模板继承,子模板中{block}区块代码建辉替换父模板中对应的代码区块

模板继承在编译时将编译成单独的一个编译文件。对比效果相似的{include}包含模板功能,模板继承的性能更高。

在子父模板{block}中的内容可以通过 {$smarty.block.parent} 或 {$smarty.block.child} 进行合并

parent.tpl:
<html>
<head>
    <title>{block name="title"}this id parent template and  set titel messages{/block}</title>
    <title>{block "title"}Default Title{/block}</title>  {* short-hand  *}
</head>
</html>

child.tpl:
{*继承父模板*}
{extends file="parent.tpl"}
{block name="title"}
    Page Title
{/block}

技术分享

//让子模板内容添加在父模板 前
child.tpl
{*继承父模板*}
{extends file="parent.tpl"}
{block name="title" prepend}
   child Page Title
{/block}


//让子模板内容添加在父模板 后
{block name="title" append}
    this is add after parent template
{/block}

技术分享

//在父模块中引入子模块
parent.tpl:
<html>
<head>
    <title>{block name="title"}The {$smarty.block.child} was inserted here{/block}</title>
</head>
</html>

child.tpl:
{extends file="parent.tpl"}
{block name="title"}
    Child Title
{/block}
//在子模块中引入父模块用
 {$smarty.block.parent}

{extend}

继承父模板,在继承时必须放在第一行。

如果想要扩展父模板时只能,通过{block}来扩展,任何其他的模板内容扩展将被忽略

{include}

用于载入其他模板到当前模板,包含模板可用的变量,在当前模板也可以用。

当文件不在 $template_dir 目录下时,就可以使用{include}进行包含

可以向包含模板传递变量,只复值不输出({assgin})

可设置缓存时间(cache_lifetime)

可以设置编译得ID(compile_id)

可设置缓存的ID(cache_id)

links.tpl
<div id="box">
<h3>{$title}{/h3>
<ul>
{foreach from=$links item=l}
.. do stuff  ...
</foreach}
</ul>
</div>

index.php
{include ‘links.tpl‘ title=‘Newest links‘ links=$link_array}
{* body of template goes here *}
{include ‘footer.tpl‘ foo=‘bar‘}

Smarty3——内置函数

标签:_id   append   file   sage   assign   str   code   body   cal   

原文地址:http://www.cnblogs.com/webph/p/6522004.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!