Home > Article > Backend Development > How to execute php code in smarty template_PHP tutorial
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:
Excerpted from Desert Snail80