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 相关文章推荐
Search Engine Friendly的URL设计
Oct 09 PHP
PHP通用检测函数集合
Nov 25 PHP
据说是雅虎的一份PHP面试题附答案
Jan 07 PHP
用php将任何格式视频转为flv的代码
Sep 03 PHP
php遍历目录与文件夹的多种方法详解
Nov 14 PHP
php 购物车完整实现代码
Jun 05 PHP
php生成图片缩略图的方法
Apr 07 PHP
PHP中的随机性 你觉得自己幸运吗?
Jan 22 PHP
基于thinkPHP类的插入数据库操作功能示例
Jan 06 PHP
PHP 应用容器化以及部署方法
Feb 12 PHP
实例介绍PHP中zip_open()函数用法
Feb 15 PHP
PHP日期和时间函数的使用示例详解
Aug 06 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实现多条件查询实例代码
2010/07/17 PHP
30 个很棒的PHP开源CMS内容管理系统小结
2011/10/14 PHP
php统计数组元素个数的方法
2015/07/02 PHP
PHP curl模拟登录带验证码的网站
2015/11/30 PHP
PHP实现图片上传并压缩
2015/12/22 PHP
php中array_column函数简单实现方法
2016/07/11 PHP
php实现网页端验证码功能
2017/07/11 PHP
php基于协程实现异步的方法分析
2019/07/17 PHP
编写Js代码要注意的几条规则
2010/09/10 Javascript
jquery多行滚动/向左或向上滚动/响应鼠标实现思路及代码
2013/01/23 Javascript
document.getElementById获取控件对象为空的解决方法
2013/11/20 Javascript
JavaScript中诡异的delete操作符
2015/03/12 Javascript
jQuery Ajax 全局调用封装实例代码详解
2016/06/02 Javascript
Angular+Node生成随机数的方法
2017/06/16 Javascript
vue项目打包后怎样优雅的解决跨域
2019/05/26 Javascript
React+Redux实现简单的待办事项列表ToDoList
2019/09/29 Javascript
[01:09:50]VP vs Pain 2018国际邀请赛小组赛BO2 第二场
2018/08/20 DOTA
Python备份Mysql脚本
2008/08/11 Python
python修改字典内key对应值的方法
2015/07/11 Python
Python和JavaScript间代码转换的4个工具
2016/02/22 Python
教你使用python画一朵花送女朋友
2018/03/29 Python
python的等深分箱实例
2019/11/22 Python
python创建ArcGIS shape文件的实现
2019/12/06 Python
浅谈keras使用预训练模型vgg16分类,损失和准确度不变
2020/07/02 Python
Python制作一个仿QQ办公版的图形登录界面
2020/09/22 Python
python连接手机自动搜集蚂蚁森林能量的实现代码
2021/02/24 Python
纯CSS和jQuery实现的在页面顶部显示的进度条效果2例(仿手机浏览器进度条效果)
2014/04/16 HTML / CSS
DJI大疆德国官方商城:大疆无人机
2018/09/01 全球购物
Etam俄罗斯:法国女士内衣和家居服网上商店
2019/10/30 全球购物
"引用"与多态的关系
2013/02/01 面试题
模具专业推荐信
2013/10/30 职场文书
中学生民族团结演讲稿
2014/08/27 职场文书
演讲开头怎么书写?
2019/08/06 职场文书
2019年共青团工作条例最新版
2019/11/12 职场文书
详解Vue3使用axios的配置教程
2022/04/29 Vue.js
Win11控制面板快捷键是什么?Win11打开控制面板的方法汇总
2022/07/07 数码科技