smarty模板的使用方法实例分析


Posted in PHP onSeptember 18, 2019

本文实例讲述了smarty模板的使用方法。分享给大家供大家参考,具体如下:

这里以smarty3为例

首先, 在官网下载smarty3模板文件,然后解压。

在解压之后的文件夹中,libs是smarty模板的核心文件,demo里面有示例程序。

我们把libs文件夹复制到我们的工作目录,然后重命名为smarty。

smarty模板的使用方法实例分析

假设我们在controller目录下的index.php中使用smarty模板。

index.php

<?php
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/';  //缓存目录
$list = range('A', 'D');
$smarty->assign("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类

<?php
require '../smarty/Smarty.class.php';
class mySmarty extends Smarty{
  public function __construct(array $options = array()){
    parent::__construct($options);
    $this->debugging = false; //开启debug模式
    $this->caching = true; //开启缓存
    $this->cache_lifetime = 120;  //缓存时间
    $this->left_delimiter = '<{'; //左定界符
    $this->right_delimiter = '}>'; //右定界符
    $this->setTemplateDir(__DIR__.'/../view/');  //视图目录
    $this->setCompileDir(__DIR__ . '/../smarty/compile/'); //编译目录
    $this->setConfigDir(__DIR__ . '/../smarty/configs/'); //配置目录
    $this->setCacheDir(__DIR__ . '/../smarty/cache/'); //缓存目录
  }
}

此时,controller里面的index.php代码可优化为:

<?php
require 'mySmarty.php';
$smarty = new mySmarty;
$list = range('A', 'D');
$smarty->assign("list", $list);
$smarty->assign("name", "zhezhao");
$smarty->display('index.html');

最后送上福利:smarty3 chm官方文档

希望本文所述对大家基于smarty模板的PHP程序设计有所帮助。

PHP 相关文章推荐
Extended CHM PHP 语法手册之 DIY
Oct 09 PHP
一些使用频率比较高的php函数
Oct 03 PHP
memcache命令启动参数中文解释
Jan 13 PHP
php的memcache类分享(memcache队列)
Mar 26 PHP
PHP实现对站点内容外部链接的过滤方法
Sep 10 PHP
php+xml编程之xpath的应用实例
Jan 24 PHP
php实现的一个简单json rpc框架实例
Mar 30 PHP
php求一个网段开始与结束IP地址的方法
Jul 09 PHP
PHP预定义变量9大超全局数组用法详解
Apr 23 PHP
微信公众号开发之语音消息识别php代码
Aug 08 PHP
PHP互换两个变量值的方法(不用第三变量)
Nov 14 PHP
利用PHP判断文件是否为图片的方法总结
Jan 06 PHP
PHP MVC框架中类的自动加载机制实例分析
Sep 18 #PHP
PHP切割整数工具类似微信红包金额分配的思路详解
Sep 18 #PHP
php实现多站点共用session实现单点登录的方法详解
Sep 18 #PHP
PHP实现批量修改文件名的方法示例
Sep 18 #PHP
php DES加密算法实例分析
Sep 18 #PHP
php实现QQ小程序发送模板消息功能
Sep 18 #PHP
php文件后缀不强制为.php的实操方法
Sep 18 #PHP
You might like
5.PHP的其他功能
2006/10/09 PHP
PHP数组内存耗用太多问题的解决方法
2010/04/05 PHP
url decode problem 解决方法
2011/12/26 PHP
php版微信小店调用api示例代码
2016/11/12 PHP
PHP内置加密函数详解
2016/11/20 PHP
PHP类与对象后期静态绑定操作实例详解
2018/12/20 PHP
PHP设计模式之工厂模式(Factory Pattern)的讲解
2019/03/21 PHP
设定php简写功能的方法
2019/11/28 PHP
JavaScript保存并运算页面中数字类型变量的写法
2015/07/06 Javascript
AngualrJS中的Directive制作一个菜单
2016/01/26 Javascript
javascript和jQuery实现网页实时聊天的ajax长轮询
2016/07/20 Javascript
vuex的简单使用教程
2018/02/02 Javascript
javascript变量提升和闭包理解
2018/03/12 Javascript
jQuery实现动画、消失、显现、渐出、渐入效果示例
2018/09/06 jQuery
JS基于开关思想实现的数组去重功能【案例】
2019/02/18 Javascript
jQuery实现弹幕特效
2019/11/29 jQuery
JavaScript实现移动端弹窗后禁止滚动
2020/05/25 Javascript
Element Carousel 走马灯的具体实现
2020/07/26 Javascript
Python pass详细介绍及实例代码
2016/11/24 Python
python 脚本生成随机 字母 + 数字密码功能
2018/05/26 Python
python 并发编程 非阻塞IO模型原理解析
2019/08/20 Python
Python 单例设计模式用法实例分析
2019/09/23 Python
Python基于pyecharts实现关联图绘制
2020/03/27 Python
python实现输入三角形边长自动作图求面积案例
2020/04/12 Python
打印tensorflow恢复模型中所有变量与操作节点方式
2020/05/26 Python
CSS3 3D旋转rotate效果实例介绍
2016/05/03 HTML / CSS
德国最大的网上鞋店之一:Schuhe24.de
2017/06/10 全球购物
Java面试题:请说出如下代码的输出结果
2013/04/22 面试题
大学毕业登记表自我鉴定
2013/10/09 职场文书
本科毕业生的求职信范文
2013/11/20 职场文书
水利公司纪检监察自我鉴定
2014/02/25 职场文书
学生检讨书
2015/01/27 职场文书
2015年学生会干事工作总结
2015/04/09 职场文书
三十年同学聚会致辞
2015/07/28 职场文书
2016毕业实习单位评语大全
2015/12/01 职场文书
python非标准时间的转换
2021/07/25 Python