• 冒险村物语
  • 英雄无敌3塔防
  • 驾考家园手游

如何在smarty模板中执行php代码

2014-09-05

Smarty模板主要的目的是分离逻辑层和表现层,所以在模板中不应该包含逻辑部分,逻辑层也不应该含有HTML。要在模板中插入逻辑程序的这种做法“非常”不被推荐,在你的case中。
如果你真正的需要在模板中使用脚本程序,smarty也提供了{php}标签,允许programmer在表现层混入php代码(再次提示:这种不利于逻辑层与表现层的分离,违背了程序与结构的分离)。看一看如何在模板中插入php代码:
phpCode.tpl:
1. {php}
2.   echo "There is php code in the template of smarty";
3.   for($i=1 ; $i<=3; $i++){
4.      echo "but it's better to avoid php code in your case!";
5.   }
6. {/php}
phpCode.php
 
1. <?
2. include("libs/smarty.class.php");
3. $smarty = new smarty();
4. $smarty->display("phpCode.tpl")
5. ?>
效果图:

 
 


摘自 沙漠的蜗牛80
(免责声明:文章内容如涉及作品内容、版权和其它问题,请及时与我们联系,我们将在第一时间删除内容,文章内容仅供参考)

人气推荐

知识阅读

精彩推荐

  • 游戏
  • 软件
查看更多>>