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 相关文章推荐
关于在php.ini中添加extension=php_mysqli.dll指令的说明
Jun 14 PHP
PHP ajax 分页类代码
Nov 13 PHP
php is_file()和is_dir()用于遍历目录时用法注意事项
Mar 02 PHP
PHP多个版本的分析解释
Jul 21 PHP
php中使用exec,system等函数调用系统命令的方法(不建议使用,可导致安全问题)
Sep 07 PHP
mysql,mysqli,PDO的各自不同介绍
Sep 19 PHP
解析PHP中常见的mongodb查询操作
Jun 20 PHP
PHP中preg_match正则匹配中的/u、/i、/s含义
Apr 17 PHP
PHP内存缓存功能memcached示例
Oct 19 PHP
给大家分享几个常用的PHP函数
Jan 15 PHP
PHP实现redis限制单ip、单用户的访问次数功能示例
Jun 16 PHP
php双向队列实例讲解
Nov 17 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
星际初学者游戏中永远要做的事
2020/03/04 星际争霸
PHP 修复未正常关闭的HTML标签实现代码(支持嵌套和就近闭合)
2012/06/07 PHP
php类中的各种拦截器用法分析
2014/11/03 PHP
php curl 模拟登录并获取数据实例详解
2016/12/22 PHP
Yii框架应用组件用法实例分析
2020/05/15 PHP
php命令行模式代码实例详解
2021/02/26 PHP
javascript 获取url参数和script标签中获取url参数函数代码
2010/01/22 Javascript
javascript使用for循环批量注册的事件不能正确获取索引值的解决方法
2014/12/20 Javascript
JavaScript实现添加、查找、删除元素
2015/07/02 Javascript
JS实现图片的不间断连续滚动的简单实例
2016/06/03 Javascript
jQuery组件easyui对话框实现代码
2016/08/25 Javascript
jQuery无刷新上传之uploadify简单代码
2017/01/17 Javascript
jquery仿京东商品放大浏览页面
2017/06/06 jQuery
JS函数节流和函数防抖问题分析
2017/12/18 Javascript
详解vue-cli中模拟数据的两种方法
2018/07/03 Javascript
ElementUI Tag组件实现多标签生成的方法示例
2019/07/08 Javascript
javascript导出csv文件(excel)的方法示例
2019/08/25 Javascript
使用element-ui +Vue 解决 table 里包含表单验证的问题
2020/07/17 Javascript
[00:44]TI7不朽珍藏III——军团指挥官不朽展示
2017/07/15 DOTA
Google开源的Python格式化工具YAPF的安装和使用教程
2016/05/31 Python
Python IDE Pycharm中的快捷键列表用法
2019/08/08 Python
对Pytorch神经网络初始化kaiming分布详解
2019/08/18 Python
python爬虫开发之Beautiful Soup模块从安装到详细使用方法与实例
2020/03/09 Python
卸载tensorflow-cpu重装tensorflow-gpu操作
2020/06/23 Python
python中通过pip安装库文件时出现“EnvironmentError: [WinError 5] 拒绝访问”的问题及解决方案
2020/08/11 Python
Python如何急速下载第三方库详解
2020/11/02 Python
Python通过Schema实现数据验证方式
2020/11/12 Python
python 使用OpenCV进行简单的人像分割与合成
2021/02/02 Python
澳大利亚相机之家:Camera House
2017/11/30 全球购物
NFL加拿大官方网上商店:NHLShop.ca
2019/03/12 全球购物
2014年会演讲稿范文
2014/01/06 职场文书
公司周年庆典策划方案
2014/05/17 职场文书
个人委托函范文
2015/01/29 职场文书
房屋租赁意向书范本
2015/05/09 职场文书
Filebeat 采集 Nginx 日志的方法
2021/03/31 Servers
IDEA2021.2配置docker如何将springboot项目打成镜像一键发布部署
2021/09/25 Java/Android