无为清净楼资源网 Design By www.qnjia.com
本文实例讲述了smarty模板的使用方法。分享给大家供大家参考,具体如下:
这里以smarty3为例
首先, 在官网下载smarty3模板文件,然后解压。
在解压之后的文件夹中,libs是smarty模板的核心文件,demo里面有示例程序。
我们把libs文件夹复制到我们的工作目录,然后重命名为smarty。
假设我们在controller目录下的index.php中使用smarty模板。
index.php
<"list", $list); $smarty->assign("name", "zhezhao"); $smarty->display('index.html');
模板文件index.html
<html> <head> <title></title> </head> <body> <p><h1><{$name}></h1></p> <{foreach $list as $k=>$v }> <p><h1><{$k}> : <{$v}></h1></p> <{/foreach}> </body> </html>
上述方法的优点是使用起来配置比较简单,缺点也是显而易见的,我们controller目录下可能有很多页面调用smarty模板,在每个页面都需要将上述方法配置一遍。
解决方法有两种:
将smarty模板的配置信息写到一个文件中,然后其他页面可以通过包含该文件使用smarty对象。
require '../smarty/Smarty.class.php'; $smarty = new Smarty; $smarty->debugging = false; //开启debug模式 $smarty->caching = true; //开启缓存 $smarty->cache_lifetime = 120; //缓存时间 $smarty->left_delimiter = '<{'; //左定界符 $smarty->right_delimiter = '}>'; //右定界符 $smarty->template_dir = __DIR__.'/../view/'; //视图目录 $smarty->compile_dir = __DIR__ . '/../smarty/compile/'; //编译目录 $smarty->config_dir = __DIR__ . '/../smarty/configs/'; //配置目录 $smarty->cache_dir = __DIR__ . '/../smarty/cache/'; //缓存目录
我们自己编写一个类,继承自Smarty类,然后将配置信息写在构造函数中。
我们编写mySmarty类
<"htmlcode"><"list", $list); $smarty->assign("name", "zhezhao"); $smarty->display('index.html');最后送上福利:smarty3 chm官方文档。
更多关于Smarty相关内容感兴趣的读者可查看本站专题:《smarty模板入门基础教程》、《PHP模板技术总结》、《PHP基于pdo操作数据库技巧总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于smarty模板的PHP程序设计有所帮助。
标签:
smarty,模板
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 刘宇.2024-宇宙诗人【哔哟哔哟文化】【FLAC分轨】
- 《全景声白噪音》沉浸式大自然助眠 带你听自然的声音[mp3]
- 纯音入心系列纯音乐《古筝精选 十指弹一赏心曲一心奏出世人心》1CD[MP3][582MB]
- 纯音入心系列纯音乐《古筝与现代音乐的交织》1CD[MP3][96MB]
- 王俪婷.2011-ROMANCE【华研国际】【WAV+CUE】
- 狮子合唱团.2016-LION(引进版)【华纳】【WAV+CUE】
- 范文芳.1996-FNNTASY【HYPE】【WAV+CUE】
- 纯音入心系列纯音乐《韵味古筝曲》1CD[MP3][146.2MB]
- 纯音入心系列纯音乐《古筝系列-传统乐曲》1CD[MP3][974.7MB]
- 纯音入心系列纯音乐《古筝系列-弦凝指咽声停处》1CD[MP3][656.5MB]
- 群星.1994-大烂片2辑【派森】【WAV+CUE】
- 吴倩莲.1997-望爱【EMI百代】【WAV+CUE】
- 杨千嬅.2002-万紫千红演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郭采洁.2015-Begin.Again爱造飞鸡【华纳】【FLAC分轨】
- 许志安.2011-ON.AND.ON【东亚】【WAV+CUE】