Smarty模板引擎的使用
Jul 29, 2016 am 09:05 AM
path
php
root
smarty
一、为什么使用Smarty?
- PHP代码与HTML代码混合在一起,很混乱。Smarty将表现层(前端)和逻辑层(后端)分开。
- 利用smarty缓存技术,有效提高访问速度。
二、Smarty的原理&使用
原理可以用一张图来表示
使用方法
1:在index.php中引入smarty的入口文件:Smarty.class.php
2:实例化一个smarty类;
3:通过对象配置smarty的编译目录、模板目录,缓存目录等等。
4:可将配置文件专门放在一个文件里。
代码
index.php文件
<code><span><span><?php </span><span>//引入配置文件</span><span>require</span><span>'smarty/smarty.inc.php'</span>; <span>//注册变量</span><span>$name</span>=<span>"eric1122"</span>; <span>$smarty</span>->assign(<span>"name"</span>,<span>$name</span>); <span>//引入模板</span><span>$smarty</span>->display(<span>"index.tpl"</span>); <span>?></span></span></span></code>
Copier après la connexion
smarty.inc.php文件
<code><span></span>php <span>/** * smarty 配置文件 */</span><span>//创建一个实际路径,加快速度</span><span><span>define</span></span>(<span>"ROOT_PATH"</span>,dirname(__FILE__)); <span>//引入smarty入口文件</span><span>require</span>(ROOT_PATH<span>.</span><span>"/Smarty.class.php"</span>); <span>//实例化一个smarty</span><span>$smarty</span><span>=</span><span>new</span> Smarty(); <span>//配置文件</span><span>//模板目录</span><span>$smarty</span><span>-></span>template_dir<span>=</span>ROOT_PATH<span>.</span><span>"/templates"</span>; <span>//缓存目录</span><span>$smarty</span><span>-></span>cache_dir<span>=</span>ROOT_PATH<span>.</span><span>"/cache"</span>; <span>//配置目录</span><span>$smarty</span><span>-></span>config_dir<span>=</span>ROOT_PATH<span>.</span><span>"/configs"</span>; <span>//配置编译目录</span><span>$smarty</span><span>-></span>compile_dir<span>=</span>ROOT_PATH<span>.</span><span>"/compile"</span>; <span>?></span></code>
Copier après la connexion
以上就介绍了Smarty模板引擎的使用,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Article chaud
Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines
By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines
By DDD
R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques semaines
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines
By 尊渡假赌尊渡假赌尊渡假赌

Outils chauds Tags

Article chaud
Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines
By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines
By DDD
R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques semaines
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines
By 尊渡假赌尊渡假赌尊渡假赌

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian

Comment configurer Visual Studio Code (VS Code) pour le développement PHP
