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 define函数的使用说明
Aug 27 PHP
一些使用频率比较高的php函数
Oct 03 PHP
php随机输出名人名言的代码
Oct 07 PHP
浅析PHP 按位与或 (^ 、&amp;)
Jun 21 PHP
ThinkPHP的cookie和session冲突造成Cookie不能使用的解决方法
Jul 01 PHP
ThinkPHP实现多数据库连接的解决方法
Jul 01 PHP
PHP使用内置函数生成图片的方法详解
May 09 PHP
PHP实现更改hosts文件的方法示例
Aug 08 PHP
PHP基于堆栈实现的高级计算器功能示例
Sep 15 PHP
php压缩文件夹最新版
Jul 18 PHP
PHP快速排序算法实现的原理及代码详解
Apr 03 PHP
Yii框架ACF(accessController)简单权限控制操作示例
Apr 26 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
PHP5 面向对象(学习记录)
2009/12/02 PHP
PHP批量生成静态HTML的简单原理和方法
2014/04/20 PHP
php选择排序法实现数组排序实例分析
2015/02/16 PHP
PHP中spl_autoload_register()函数用法实例详解
2016/07/18 PHP
Joomla框架实现字符串截取的方法示例
2017/07/18 PHP
CL vs ForZe BO5 第五场 2.13
2021/03/10 DOTA
js 跨域和ajax 跨域问题小结
2009/07/01 Javascript
jQuery1.5.1 animate方法源码阅读
2011/04/05 Javascript
实现动画效果核心方式的js代码
2013/09/27 Javascript
Jquery 复选框取值兼容FF和IE8(测试有效)
2013/10/29 Javascript
原生Javascript封装的一个AJAX函数分享
2014/10/11 Javascript
js给网页加上背景音乐及选择音效的方法
2015/03/03 Javascript
Javascript编写俄罗斯方块思路及实例
2015/07/07 Javascript
Javascript动画效果(3)
2016/10/11 Javascript
js实现界面向原生界面发消息并跳转功能
2016/11/22 Javascript
BootStrap按钮标签及基本样式
2016/11/23 Javascript
JS多文件上传的实例代码
2017/01/11 Javascript
javascript中replace使用方法总结
2017/03/01 Javascript
详解nodejs微信jssdk后端接口
2017/05/25 NodeJs
微信小程序WebSocket实现聊天对话功能
2018/07/06 Javascript
在Layui中操作数据表格,给指定单元格添加事件示例
2019/10/26 Javascript
Python下线程之间的共享和释放示例
2015/05/04 Python
基于进程内通讯的python聊天室实现方法
2015/06/28 Python
Python利用正则表达式匹配并截取指定子串及去重的方法
2015/07/30 Python
Python中矩阵创建和矩阵运算方法
2018/08/04 Python
Python 单元测试(unittest)的使用小结
2018/11/14 Python
python自动发微信监控报警
2019/09/06 Python
python修改FTP服务器上的文件名
2019/09/11 Python
使用python脚本自动生成K8S-YAML的方法示例
2020/07/12 Python
聊聊python在linux下与windows下导入模块的区别说明
2021/03/03 Python
HTML5移动端开发中的Viewport标签及相关CSS用法解析
2016/04/15 HTML / CSS
Boston Proper官网:美国女装品牌
2017/10/30 全球购物
小学教师师德师风个人整改措施
2014/09/18 职场文书
《田忌赛马》教学反思
2016/02/19 职场文书
css3 实现文字闪烁效果的三种方式示例代码
2021/04/25 HTML / CSS
Python四款GUI图形界面库介绍
2022/06/05 Python