CodeIgniter集成smarty的方法详解


Posted in PHP onMay 26, 2016

本文实例讲述了CodeIgniter集成smarty的方法。分享给大家供大家参考,具体步骤如下:

1.下载smarty

解压到ci的libraries目录 如:

ci/application/libraries/Smarty-2.6.20

2.编写Mysmarty.php 自己的类库文件

代码如下:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
require "Smarty-2.6.20/libs/Smarty.class.php";
/**
* @file system/application/libraries/Mysmarty.php
*/
class Mysmarty extends Smarty
{
  function Mysmarty()
  {
    $this->Smarty();
    $config =& get_config();
    // absolute path prevents "template not found" errors
    $this->template_dir = (!empty($config['smarty_template_dir']) ? $config['smarty_template_dir'] : BASEPATH . 'application/views/');
    $this->compile_dir = (!empty($config['smarty_compile_dir']) ? $config['smarty_compile_dir'] : BASEPATH . 'cache/'); 
    //use CI's cache folder
    if (function_exists('site_url')) {
      // URL helper required
      $this->assign("site_url", site_url()); // so we can get the full path to CI easily
    }
  }
  /**
  * @param $resource_name string
  * @param $params array holds params that will be passed to the template
  * @desc loads the template
  */
  function view($resource_name, $params = array())  {
    if (strpos($resource_name, '.') === false) {
      $resource_name .= '.html';
    }
    if (is_array($params) && count($params)) {
      foreach ($params as $key => $value) {
        $this->assign($key, $value);
      }
    }
    // check if the template file exists.
    if (!is_file($this->template_dir . $resource_name)) {
      show_error("template: [$resource_name] cannot be found.");
    }
    return parent::display($resource_name);
  }
} // END class smarty_library
?>

3.在autoload.php让ci自动加载smarty

$autoload['libraries'] = array('database', 'mysmarty');

或者 使用模板时再自己加载smarty

$this->load->library("mysmarty");

4.smarty变量赋值 display模板

$this->mysmarty->assign('test', 'Hello World.');
$this->mysmarty->view('smarty');

注:images css 等外部资源文件 放在ci系统文件夹外 网站根目录下

最好用:

$this->load->helper('url');

base_url()来访问:

base_url()."images/xxx.jpg"

不要放到system里

PHP 相关文章推荐
坏狼的PHP学习教程之第2天
Jun 15 PHP
php在线打包程序源码
Jul 27 PHP
全局记录程序片段的运行时间 正确找到程序逻辑耗时多的断点
Jan 06 PHP
javascript,php获取函数参数对象的代码
Feb 03 PHP
PHP 年龄计算函数(精确到天)
Jun 07 PHP
php之XML转数组函数的详解
Jun 07 PHP
symfony表单与页面实现技巧
Jan 26 PHP
php获取字符串中各个字符出现次数的方法
Feb 23 PHP
Nginx下配置codeigniter框架方法
Apr 07 PHP
PHP封装的MSSql操作类完整实例
May 26 PHP
PHP unlink与rmdir删除目录及目录下所有文件实例代码
Feb 07 PHP
laravel框架创建授权策略实例分析
Nov 22 PHP
PHPExcel简单读取excel文件示例
May 26 #PHP
php快速排序原理与实现方法分析
May 26 #PHP
深入理解PHP中的empty和isset函数
May 26 #PHP
CodeIgniter常用知识点小结
May 26 #PHP
php数组函数array_walk用法示例
May 26 #PHP
PHP发送AT指令实例代码
May 26 #PHP
PHP+sqlite数据库操作示例(创建/打开/插入/检索)
May 26 #PHP
You might like
很好用的PHP数据库类
2009/05/27 PHP
PHP以指定字段为索引返回数据库所取的数据数组
2013/06/30 PHP
PHP中使用strpos函数实现屏蔽敏感关键字功能
2014/08/21 PHP
PHP实现求连续子数组最大和问题2种解决方法
2017/12/26 PHP
利用PHP内置SERVER开启web服务(本地开发使用)
2021/03/09 PHP
jquery $.ajax入门应用一
2008/11/19 Javascript
JavaScript高级程序设计 扩展--关于动态原型
2010/11/09 Javascript
javascript模版引擎-tmpl的bug修复与性能优化分析
2011/10/23 Javascript
公共js在页面底部加载的注意事项介绍
2013/07/18 Javascript
jQuery实现瀑布流的取巧做法分享
2015/01/12 Javascript
理解和运用JavaScript的闭包机制
2015/08/13 Javascript
js生成随机数的方法实例
2015/10/16 Javascript
js实现索引图片切换效果
2015/11/21 Javascript
基于jquery实现页面滚动时顶部导航显示隐藏
2020/04/20 Javascript
jQuery实现发送验证码并60秒倒计时功能
2016/11/25 Javascript
p5.js入门教程和基本形状绘制
2018/03/15 Javascript
JAVA面试题 static关键字详解
2019/07/16 Javascript
微信小程序云函数添加数据到数据库的方法
2020/03/04 Javascript
Python比较两个图片相似度的方法
2015/03/13 Python
python实现向ppt文件里插入新幻灯片页面的方法
2015/04/28 Python
Python3.遍历某文件夹提取特定文件名的实例
2018/04/26 Python
Django学习教程之静态文件的调用详解
2018/05/08 Python
使用Python串口实时显示数据并绘图的例子
2019/12/26 Python
python pptx复制指定页的ppt教程
2020/02/14 Python
详解python tkinter 图片插入问题
2020/09/03 Python
html5 canvas 实现光线沿不规则路径运动
2020/04/20 HTML / CSS
年度考核评语
2014/01/19 职场文书
百年校庆节目主持词
2014/03/27 职场文书
关于梦想的演讲稿
2014/05/05 职场文书
七夕情人节促销方案
2014/06/07 职场文书
法人委托书
2014/07/31 职场文书
群众路线剖析材料范文
2014/10/09 职场文书
财务工作检讨书
2014/10/29 职场文书
优秀员工事迹材料
2014/12/20 职场文书
如何使JavaScript休眠或等待
2021/04/27 Javascript
详解Oracle数据库中自带的所有表结构(sql代码)
2021/11/20 Oracle