几个smarty小技巧

1,capture标签

capture的中文意思是抓取,它的作用是抓取模板输出的数据,当我们需要它的时候,调用它,以得到抓取数据的目的。例子:

Java代码   收藏代码
  1. {capture name=test}  
  2.     <img src="testimg.jpg">  
  3. {/capture}  
  4. <div class="image">  
  5.     {$smarty.capture.test}  
  6. </div>  

 说明:
在{capture name="test"}和{/capture}之间的内容被存储到变量$test中,该变量由name属性指定.在模板中通过 $smarty.capture.test 访问该变量.如果没有指定name 属性,函数默认将使用"default" 作为参数,这一点很jQuery中的clone

 

2,config_load标签

config_load可以直接将文件中的内容读取出来,这样可以省掉assign这一步。

Java代码   收藏代码
  1. test.csv:  
  2.   
  3. pageTitle = "config_load_test"  
  4. bodyBgColor = "#eeeeee"  
  5. img = "girl.jpg"  
  6. width="100"  
  7. height="100"  
  8.   
  9. index.tpl:  
  10.   
  11. {config_load file="test.csv"}  
  12. <html>  
  13. <title>{#pageTitle#}</title>  
  14. <body bgcolor="{#bodyBgColor#}">  
  15. <img src="{#img#}" width="{#width#}" height="{#height#}">  
  16. </body>  
  17. </html>  

上述过程中如果出现这样的问题Warning: Smarty error: unable to read resource, 请查看一下,你的test.csv是不是放在smarty的配置目录中,默认配置目录是configs

Java代码   收藏代码
  1. /** 
  2.  * The directory where config files are located. 
  3.  * 
  4.  * @var string 
  5.  */  
  6. var $config_dir      =  'configs';  

 

 3,literal标签的使用

做web开发,难免会写一些JS,css,jquery代码。js和jquery里面都会{}这样的符号,smarty会不会把它理解成php的变量呢?如果你不加literal标签的话,smarty肯定会把它理解变量了,加了就不会,例如:

Java代码   收藏代码
  1. {literal}  
  2. function getAbsLeft(e){  
  3.     var l=e.offsetLeft;  
  4.     while(e=e.offsetParent)l+=e.offsetLeft;  
  5.     return l;  
  6. }  
  7.   
  8. function getAbsTop(e){  
  9.     var t=e.offsetTop;  
  10.     while(e=e.offsetParent)t+=e.offsetTop;  
  11.     return t;  
  12. }  
  13. {/literal}  
  14. {literal}  
  15. <style type="text/css">  
  16.     body {  
  17.         color: #FFF;  
  18.     }  
  19. </style>  
  20. {/literal}  

 

4,php标签

smarty模版使用php标签,如何获取模版变量.模版变量全部存在smarty的一个对象里面

Java代码   收藏代码
  1. {php}  
  2. $assign = $this->_tpl_vars[assign];  
  3. {/php}  

当你习惯了assign后,你有没有想过,在模板文件里面直接写php代码呢 ,我想有的时候你肯定很想吧。例如:

Java代码   收藏代码
  1. {php}  
  2.     global $result;  
  3.     foreach($result as $key=>$value){  
  4.         echo "key=$key,value=>$value<br>";  
  5.     }  
  6. {/php}  

 

5 strip标签

strip标签去除标签内的空格和回车,这一点我觉得,做手机开发的朋友肯定用的到,因为全角空格有可能会导致整个页面错乱,甚至是一个空白页面。手机屏幕小,估计用smarty的可能性也比较小。

Java代码   收藏代码
  1. {strip}  
  2. <div>  
  3.  <font color="red">strip</font>  
  4. </div>  
  5. {/strip}  

 

6,fetch标签

fetch标签根php的file_get_contents挺想的,都可以把文件中的内容读出来,并且是个字符串的形势

Java代码   收藏代码
  1. {fetch file="./aaaa.txt" assign="result"}  
  2. {if is_array($result)}  
  3.  <b>is array</b>  
  4. {else if}  
  5.  <b>not array</b>  
  6. {/if}  
7,smarty调用php函数

smarty模板里调用php的内置函数{'param1'|functionName:'param2':'param3'} 

Java代码   收藏代码
  1. {$colname|trim}  //or {trim($colname)}  
  2. {$colname|iconv:'utf-8':'gbk'}  
echo trim(param1)
调用自定义的php函数$smarty->register_function('len',"customerfunction");
8.smarty保留变量
Java代码   收藏代码
  1. {$smarty.get.page}             {* PHP方式:$_GET["page"] *}   
  2. {$smarty.post.page}            {* PHP方式:$_POST["page"] *}   
  3. {$smarty.cookies.username}     {* PHP方式:$_COOKIE["username"] *}   
  4. {$smarty.session.id}           {* PHP方式:$_SESSION["id"] *}   
  5. {$smarty.server.SERVER_NAME}   {* PHP方式:$_SERVER["SERVER_NAME"] *}   
  6. {$smarty.env.PATH}             {* PHP方式:$_ENV["PATH"] *}   
  7. {$smarty.request.username}     {* PHP方式:$_REQUEST["username"] *}   
  8. {$smarty.const.__FILE__}       {* 通过保留变量数组直接输出系统常量 *}  
  9. {$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'}  {* 取得当前时间戳 *}  
9.用于在模板执行过程中设置模板变量
Java代码   收藏代码
  1. {assign var='foo' value='Smarty'}  
10.当变量为空时,设置默认值
Java代码   收藏代码
  1. {$var|default:"no value"}  
{section name = name loop = $varName[, start = $start, step = $step, max = $max, show = true]} 
name: section的名称,不用加$ 
$loop: 要循环的变量,在程序中要使用assign对这个变量进行操作。 
$start: 开始循环的下标,循环下标默认由0开始 
$step: 每次循环时下标的增数 
$max: 最大循环下标 
$show: boolean类型,决定是否对这个块进行显示,默认为true
循环下标:实际它的英文名称为index,是索引的意思,这里我将它译成"下标",主要是为了好理解。它表示在显示这个循环块时当前的循环索引,默认从0 开始,受$start的影响,如果将$start设为5,它也将从5开始计数,在模板设计部分我们使用过它,这是当前{section}的一个属性,调用 方式为Smarty.section.sectionName.index,这里的sectionName指的是函数原型中的name属性。

{section}块具有的属性值,分别为: 
1. index: 上边我们介绍的"循环下标",默认为0 
2. index_prev: 当前下标的前一个值,默认为-1 
3. index_next: 当前下标的下一个值,默认为1 
4. first: 是否为第一下循环 
5. last: 是否为最后一个循环 
6. iteration: 循环次数 
7. rownum: 当前的行号,iteration的另一个别名 
8. loop: 最后一个循环号,可用在section块后统计section的循环次数 
9. total: 循环次数,可用在section块后统计循环次数

Java代码   收藏代码
  1. {section name=loop loop=2019 start=2019 step=-1 }   //2019=>2000  
  2.     {if $smarty.section.loop.iteration<=19}  
  3.         <option value="{$smarty.section.loop.index}">{$smarty.section.loop.index}</option>  
  4.     {/if}  
  5. {/section}  

asfdas

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值