如何在smarty模板中执行php代码
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
