Home  >  Article  >  php教程  >  如何在smarty模板中执行php代码

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

WBOY
WBOYOriginal
2016-06-13 10:48:491488browse

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 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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn