Home  >  Article  >  Backend Development  >  How to execute php code in smarty template_PHP tutorial

How to execute php code in smarty template_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:50:29982browse

The main purpose of Smarty templates is to separate the logic layer and the presentation layer, so the template should not contain logic parts, and the logic layer should not contain HTML. Inserting logic into templates is "very" not recommended, in your case.
If you really need to use a script program in the template, smarty also provides the {php} tag, allowing the programmer to mix PHP code in the presentation layer (note again: this is not conducive to the separation of the logic layer and the presentation layer, and violates the program and structure separation). Take a look at how to insert php code in a template:
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. ?>
Rendering:

How to execute php code in smarty template_PHP tutorial



Excerpted from Desert Snail80

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478268.htmlTechArticleThe main purpose of Smarty template is to separate the logic layer and presentation layer, so the template should not contain the logic part. The layer should also not contain HTML. To insert a logic program into a template...
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