Smarty里需要注意的一些知识

一、 $name=array('one','tow','three','four'); foreach输出 <{foreach item=item from=$name}> <{$item}> <{/foreach}> 结果 one tow three four section输出 <{section name=name loop=$name}> <{$name[name]}> <{/section}> 结果one tow three four 二、 $name=array('a'=>'one','b'=>'tow','c'=>'three','d'=>'four'); 这种情况下,用foreach可以输出正常 用section没有输,这就是说,section在处理一维数组是,不能处理带非数值索引的 例:可以处理array('a','b'),array(1=>'a',2=>'b'),不能处理array('a'=>'a','b'=>'b')这样的 三、 $name=array(array('title'=>'a','body'=>'b'),array('title'=>'c','body'=>'d')); 以下都可以输出 <{foreach key=key item=item from=$name}> <{$item.title}><br> <{/foreach}> <{section name=name loop=$name}> <{$name[name].title}> <{/section}> 四、 $name=array(array('a','b'),array(‘c','d')); 以下都 可输出 <{foreach key=key item=item from=$name}> <{$item.0}><br> <{/foreach}> <{section name=name loop=$name}> <{$name[name].0}> <{/section}> ====================================================== include语句,将其它模板引入当前模板 <{include file='header.tpl'}> 两个特性 <{include file='header.tpl' assign='header'}>则不会输,而是将文件的内容给变量$header <{include file='header.tpl' title='我是标题' }>加载模板同时把$title传给模板,注意变量title不能带引号 以此方式传递的变量只能在所导入的文件中使用 insert用它导入不会被缓存的数据 使模板的一部分不被缓存. 如果打开了缓存, insert 函数却不会被缓存,每次调用页面它们都会被动态加载,即使是在缓存页面中. 该特性可以广泛应用于广告条、投票、实时天气预报、搜索结果、反馈信息等区域. 用法,在php脚本中 function insert_time($char) { echo ($char['char']); echo date('H:i:s'); } 在模板中 <{insert name='time' char='现在时间'}> 注意的是所有的insert函数以insert_命名 literal用于原样显示 <{literal}> <{$name}> <{/literal}> 会输出<{$name}>而不会替换 php 在模板中嵌入php脚本 include_php 导入带php代码的脚本 ========================================================================= 配置文件 #global appname='配置文件的测试'; [jjq] title='佳佳泉' content='佳佳泉的内容' [sina] title='新浪济南' content='新浪济南的内容' 说明[]称为节,节之外的是全局的,这些项必须在节之前定义,配置文件放在smarty配置(config_dir)指定的目录中 字符串多行时,保存在三个引号中 例 name="""我是                               两行""" 使用配置文件 <{config_load file='app.config'}>加载配置文件的全局变量 <{config_load file='app.config' section='jjq'}>加载特定的节 引用配置变量 用#或$smarty.config <{#title#}> <{$smarty.config.title}> ======================================================================================== 缓存可以生成静态 面,与编译的区别 编译减少将模板转换为php脚本的开销,但仍需要在逻辑层执行获取数据的命令 缓存则直接生成静态页,减 少上面所述的开销 编译默认是开启的,而缓存必须由开发人员显式开启 $smarty->caching=1; $smarty->cache_lifetime=60; 技巧:如果你想给某些模板设定它们自己的缓存生存时间,你可以在调用display()或fetch()函数之前,通过设置$caching = 2,然后设置$cache_lifetime为一个唯一值来实现. is_cached if(!$smarty->is_cached("index.tpl")) { // do database calls, assign vars here // 调用数据库,并对变量进行赋值 } $smarty->display("index.tpl"); ================================================================ 6.接收变量函数 $smarty.get.id:接收get过来的id的值 {{$smarty.get.id}} $smarty.post.id:接收post过来的id的值 {{$smarty.post.id}} $smarty.request.id:接收request过来的id的值 {{$smarty.request.id}} $smarty.cookies.id:接收cookie中名为id的值 {{$smarty.cookies.id}} $smarty.session.id:接收session中名为id的值 {{$smarty.session.id}} $smarty.const.FNAME:取出php中定义名为FNAME的常量: {{$smarty.const.FNAME}} $smarty.now:取出当前UNIX时间戳 :{{$smarty.now}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值