频道栏目
首页 > 资讯 > Smarty > 正文

6个smarty小技巧

16-01-07        来源:[db:作者]  
收藏   我要投稿

1,capture标签

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

查看复制打印?
{capturename=test} {/capture} {$smarty.capture.test}
{capture name=test}
{/capture}
{$smarty.capture.test}

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

2,config_load标签

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

查看复制打印?
test.csv: pageTitle="config_load_test" bodyBgColor="#eeeeee" img="girl.jpg" width="100" height="100" index.tpl: {config_loadfile="test.csv"}
test.csv:

pageTitle = "config_load_test"
bodyBgColor = "#eeeeee"
img = "girl.jpg"
width="100"
height="100"

index.tpl:

{config_load file="test.csv"}

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

查看复制打印?
/** *Thedirectorywhereconfigfilesarelocated. * *@varstring */ var$config_dir='configs';
    /**
     * The directory where config files are located.
     *
     * @var string
     */
    var $config_dir      =  'configs';

3,literal标签的使用

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

查看复制打印?
{literal} functiongetAbsLeft(e){ varl=e.offsetLeft; while(e=e.offsetParent)l+=e.offsetLeft; returnl; } functiongetAbsTop(e){ vart=e.offsetTop; while(e=e.offsetParent)t+=e.offsetTop; returnt; } {/literal}
{literal}
function getAbsLeft(e){
 var l=e.offsetLeft;
 while(e=e.offsetParent)l+=e.offsetLeft;
 return l;
} 
function getAbsTop(e){
 var t=e.offsetTop;
 while(e=e.offsetParent)t+=e.offsetTop;
 return t;
}
{/literal}

4,php标签

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

查看复制打印?
{php} global$result; foreach($resultas$key=>$value){ echo"key=$key,value=>$value
"; } {/php}
{php}
	global $result;
	foreach($result as $key=>$value){
		echo "key=$key,value=>$value
";
	}
{/php}

5,strip标签

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

查看复制打印?
{strip}
strip
{/strip}
{strip}
strip
{/strip}

6,fetch标签

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

查看复制打印?
{fetchfile="./aaaa.txt"assign="result"} {ifis_array($result)} isarray {elseif} notarray {/if}
相关TAG标签
上一篇:smarty循环foreach,section实例详解
下一篇:smarty的插件功能是smarty模板的精华
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站