此文章由ArthurXF(肖飞)倾情奉献!
  我们在使用smarty的时候,经常会碰到变量组合成新变量的时候,例如:$aaa$b,$b是可变值,我们需要得到$aaa1,$aaa2,$aaa3等变量.但是在smarty中,这样的变量连接写法是不允许的.

针对这样的问题,我给出下面几种情况的解决方案.
注:<?{和}?>是我定义的smarty定界符,等同于{和},希望大家理解.

引用
1.针对数组的变量连接问题:
<?php
$arrMType = array();
$arrMType[1] = '高交会展';
$arrMType[2] = '学术会议';
$arrData[id] = 1;
?>
在smarty中.
$arrMType[$arrData.id]是错误的.
解决方案:
<?{foreach from=$arrType key=key item=value}?>
<?{if $key == $arrData.type_id}?><?{$value}?><?{/if}?>
<?{/foreach}?>


2.针对文件引用的变量连接问题:
<?PHP
$page="index";
?>
在smarty中,下面的写法都是错误的.
<?{include file=$page.html}?> //这样不能执行成功
<?{include file=$page|cat:".html"}?> //还是不行
解决方案:
<?{include file="`$page`.html"}?>


3.针对函数传递的变量连接问题:
<?PHP
$arrGWeb['module_id'] = 'news';
?>
要在smarty中实现:
<?{url url = "/news/list?type_id=1"}?>
错误的写法:
<?{url url = "/$arrGWeb.module_id/list?type_id=1"}?>
正确的写法:
<?{url url = "/`$arrGWeb.module_id`/list?type_id=1"}?>
注意:"`"这个符号,是数字1前面的那个符号,不是单引号.


ArthurXF从业10年的CTO,受上海非凡学院邀请,进行PHP,CSS,FREEBSD等网络技能授课培训,有意向参加的,请联系.
PHP | 评论(2) | 引用(0) | 阅读(9214)
ArthurXF Email Homepage
2008/06/21 12:31
模板中可以按照第三种方法写。如果只是在模板里写字符串拼接可以写成{"cat_goods_`$cat_id`"},或者cat_good_{$cat_id}
hfly Email
2008/06/18 14:00
$GLOBALS['smarty']->assign('cat_goods_' . $cat_id, $goods);
我要实现这个功能
要在模版中怎么写啊!??
分页: 1/1 第一页 1 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]