• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    smarty模板的使用方法实例分析

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

    这里以smarty3为例

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

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

    我们把libs文件夹复制到我们的工作目录,然后重命名为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相关内容感兴趣的读者可查看本站专题:《smarty模板入门基础教程》、《PHP模板技术总结》、《PHP基于pdo操作数据库技巧总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

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

    您可能感兴趣的文章:
    • Smarty模板快速入门
    • 在smarty模板中使用PHP函数的方法
    • 模板引擎smarty工作原理以及使用示例
    • 解析smarty模板中类似for的功能实现
    • smarty模板中使用get、post、request、cookies、session变量的方法
    • PHP模板引擎Smarty的缓存使用总结
    • PHP模板引擎smarty详细介绍
    • smarty模板引擎中内建函数if、elseif和else的使用方法
    • smarty模板引擎中自定义函数的方法
    • php之Smarty模板使用方法示例详解
    • php Smarty模板生成html文档的方法
    • PHP 基于Yii框架中使用smarty模板的方法详解
    上一篇:PHP MVC框架中类的自动加载机制实例分析
    下一篇:php文件包含的几种方式总结
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

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