smarty安装及例子

WBOY
Libérer: 2016-07-29 09:05:58
original
900 Les gens l'ont consulté

环境:

smarty

1.在http://www.smarty.net/download下载最新smarty包,window选择zips,linux下选择tar.gz。以windows为例,下载后解压,如f:\smarty。

2.把解压出来的smarty目录里lib目录拷贝到test里,重命名为smarty。在test目录下,创建tpls目录,在tpls目录下,创建templates、templates_c、configs、cache目录,这几个目录分别是模板目录(必要),解析目录(必要),配置目录(可选),缓存目录(可选),

smarty的php代码和这四个目录是同一个级的,html代码放在templates下。

目录树如下

smarty安装及例子

代码部分:

1.在test/smarty下创建utf-8无bom格式的main.php,配置smarty的一些成员属性。

smarty安装及例子

 1 <?php 2 include("Smarty.class.php");
 3 define('SMARTY_ROOT', '../tpls');
 4 $tpl = new Smarty();
 5 $tpl->template_dir = SMARTY_ROOT."/templates/";//设置模板文件的存放目录
 6 $tpl->compile_dir = SMARTY_ROOT."/templates_c/";//设置编译文件的存放目录
 7 $tpl->config_dir = SMARTY_ROOT."/configs/";//设置配置文件的存放目录
 8 $tpl->cache_dir = SMARTY_ROOT."/cache/";//设置缓存文件的存放目录
 9 $tpl->caching=1;//开启缓存
10 $tpl->cache_lifetime=60*60*24;//有效时间为一天
11 $tpl->left_delimiter = '[';//smarty语言的左右结束符
12 $tpl->right_delimiter = ']';
13 ?>
Copier après la connexion

smarty安装及例子

我们知道大括号是smarty的默认定界符,但在和javascript、css等结合时可能会产生冲突,所以这里我们设定为[和]。

2.在test/tpls/templates下面新建html.tpl模板文件,就是在html中加入smarty变量。改模板相当于表现层。

html.tpl的代码如下:

smarty安装及例子

 1 
 2 
 3 <meta http-equiv="Content-type" c><span><img src="http://image.codes51.com/Article/image/20160121/20160121091057_4965.gif" alt="smarty安装及例子"></span><p>3.在test目录下创建smarty.php,该文件相当于驱动层,给上面表现层的变量赋好值,然后显示出来。</p><p>smarty.php的代码如下:</p><p></p><pre class="brush:php;toolbar:false">1 <?php 2     include("smarty/main.php");
3     $tpl->assign("title","迟到");
4     $tpl->assign("content","罚款500元!");
5     $tpl->display("tpls/templates/html.tpl");
6 ?>
Copier après la connexion

4.在浏览器中运行smarty.php即可。

以上就介绍了smarty安装及例子,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Étiquettes associées:
source:php.cn
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
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!