Ci框架整合smarty模板引擎

备注:下载smarty时,最好选择2.6版本,其他测试有坑,ci可以是2.2或其他

大体思路:将smarty封装成ci框架的一个类,然后重新配置一下smarty,这样ci框架就可以调用smarty模板引擎了

1.   将下载好的smarty包的lib文件上传到ci中的libraries 文件中,将取名称修改为smarty,在libraries文件新建cismarty.php文件,内容如下:

#require_once  记得要导入该smarty类文件

#$this->ci = & get_instance();  获取ci的超全局对象

#$this->ci->load->config('smarty');  加载配置文件

<?php
if(!defined('BASEPATH')) EXIT('No direct script asscess allowed');
require_once( APPPATH .'libraries/smarty/libs/Smarty.class.php' );
class Cismarty extends Smarty{
protected $ci;
public function __construct(){ $this->ci = & get_instance();
$this->ci->load->config('smarty');
parent::__construct();   $this->template_dir = $this->ci->config->item('template_dir');
  $this->complie_dir = $this->ci->config->item('compile_dir');
  $this->cache_dir = $this->ci->config->item('cache_dir');
  $this->config_dir = $this->ci->config->item('config_dir');
  $this->caching = $this->ci->config->item('caching');
  $this->cache_lifetime = $this->ci->config->item('lefttime');
}
}

2.   在config下新建smarty.php配置文件

#第一个操作的代码:$this->ci->load->config('smarty');将会调用到该配置文件

#这里的配置就是smarty里的配置

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['theme'] = 'default';
$config['template_dir'] = APPPATH . 'views';
$config['compile_dir'] = APPPATH . 'views/templates_c';
$config['cache_dir'] = APPPATH . 'views/cache';
$config['config_dir'] = APPPATH . 'views/configs';
$config['caching'] = false;
$config['lefttime'] = 60;
$config['left_delimiter'] = '<{';
$config['right_delimiter'] = '}>';

3.   在CI里重载smarty的 assign 和 display方法

#cismarty  一定要全部小写,不然ci找不到。之前被坑在这里了

#assign  该函数里面要调用smarty类的assign方法才行

#display   该函数里面要调用smarty类的display方法才行

<?php
if (!defined('BASEPATH')) exit('No direct access allowed.');
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
} public function assign($key,$val) {
$this->cismarty->assign($key,$val);
} public function display($html) {
$this->cismarty->display($html);
}
}

4.   修改Config文件下的autoload.php 自动加载类文件

#$autoload['libraries'] = array('cismarty ');  实现自动加载smarty类

$autoload['libraries'] = array('cismarty ');

5.   下面测试

a.   新建控制器admin_welcome.php

#assign  调用MY_Controller类里的assign方法,然后间接调用smarty的assign方法

#display  调用MY_Controller类里的display方法,然后间接调用smarty的display方法

<?php
class Welcome extends MY_Controller {
public function index(){
$data = "datadatadata";
$this->assign("data", $data);
$this->display('test.tpl');
}
}

Views 下新建test.tpl

#$data  控制器传来的值

<html>
<head>
</head>
<body>
aaaaaaaaaaaaaaaaaaaaa
{$data}
</body>
</html>

Ci框架整合smarty模板引擎的更多相关文章

  1. ThinkPHP3.2.3整合smarty模板(二)

    前言:继ThinkPHP3.2.3整合smarty模板(一)之后,继续来探讨一下tp框架整合smarty模板,看到有人在群上问到怎么使用自定义的常量,今天就具体来谈谈: 一.开发一个项目,必不可少会用 ...

  2. ci框架与smarty的整合

    ci框架与smarty的整合 来源:未知    时间:2014-10-20 11:38   阅读数:108   作者:xbdadmin [导读] Ci 和 smarty 的完美结合 Ci 结合 sma ...

  3. 深入浅出之Smarty模板引擎工作机制(一)

    深入浅出Smarty模板引擎工作机制,我们将对比使用smarty模板引擎和没使用smarty模板引擎的两种开发方式的区别,并动手开发一个自己的模板引擎,以便加深对smarty模板引擎工作机制的理解. ...

  4. smarty模板引擎

    1.    使用smarty 1.1 项目引入 // 3, 连接数据库,提取相关数据 $title = "Smarty模板引擎"; $content = "Smarty模 ...

  5. 写一个迷你版Smarty模板引擎,对认识模板引擎原理非常好(附代码)

    前些时间在看创智博客韩顺平的Smarty模板引擎教程,再结合自己跟李炎恢第二季开发中CMS系统写的tpl模板引擎.今天就写一个迷你版的Smarty引擎,虽然说我并没有深入分析过Smarty的源码,但是 ...

  6. 深入浅出之Smarty模板引擎工作机制(二)

    源代码下载地址:深入浅出之Smarty模板引擎工作机制 接下来根据以下的Smarty模板引擎原理流程图开发一个自己的模板引擎用于学习,以便加深理解. Smarty模板引擎的原理,其实是这么一个过程: ...

  7. Smarty模板引擎技术二

    Smarty模板引擎技术 内建函数 include_php内建函数 作用:载入一个php文件,将载入的文件的内容赋值给一个变量   注意:该内建函数只能在2.0中使用,如果使用的话,必须得实例化Sma ...

  8. Smarty模板引擎技术

    Smarty模板引擎技术 什么是模板引擎? 什么是Smarty模板引擎? 为何选择Smarty模板引擎? 如何使用Smarty模板引擎? 一.历史背景 场景一:回顾之前编写PHP项目的方式 //链接数 ...

  9. 迷你版 smarty --模板引擎和解析

    http://blog.ipodmp.com/archives/php-write-a-mini-smarty-template-engine/ 迷你版Smarty模板引擎目录结构如下: ① 要开发一 ...

随机推荐

  1. hadoop-2.5安装与配置

    安装之前准备4台机器:bluejoe0,bluejoe4,bluejoe5,bluejoe9 bluejoe0作为master,bluejoe4,5,9作为slave bluejoe0作为nameno ...

  2. java中的拷贝文件FileChannel

    以前用Java拷贝文件,只知道写byte数组循环拷贝,今天知道了可以用FileChannel进行拷贝,上代码: 下边是传统的byte数组拷贝方法 </pre><pre name=&q ...

  3. Java内存溢出的详细解决方案

    本文介绍了Java内存溢出的详细解决方案.本文总结内存溢出主要有两种情况,而JVM经常调用垃圾回收器解决内存堆不足的问题,但是有时仍会有内存不足的错误.作者分析了JVM内存区域组成及JVM设置虚拟内存 ...

  4. oracle中的exists 和not exists 用法 in与exists语句的效率问题

    博文来源(oracle中的exists 和not exists 用法):http://chenshuai365-163-com.iteye.com/blog/1003247 博文来源(  in与exi ...

  5. java取随机数

    一, 指定的特定几个数据集合里按“随机顺序”全部取出 一碰到随机, 可能第一个想到的是用Math.Random() 来处理, 其实java本身提供了现成的类 通过 “打乱顺序”来处理“随机”问题 方法 ...

  6. 12天学好C语言——记录我的C语言学习之路(Day 5)

    12天学好C语言--记录我的C语言学习之路 Day 5: 第五天的学习开始了,今天我们主要对几个程序进行编写,让自己充分的熟练编程语言,大量的题目会让自己变的精炼.以一个程序(program 5.1) ...

  7. C# ACM poj1008

    玛雅历 public static void Acm1008(int day, string mon, int year) { ; switch (mon) { case "pop" ...

  8. 【原创】关于MVC自己新建的 action,Controller提示找不到页面的问题

    一.实例: 1.比如我自己新建了一个~/view/Shop  文件夹下的IndexShop.aspx,那么在Controllers文件夹下就要对应一个ShopController.cs的Control ...

  9. 判断UserAgent是否来自微信

    iso: Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko ...

  10. Linux Vi的使用

    1.vi使用三模式:一般模式,插入模式,命令模式 保存和退出vi: 命令模式下 :w 保存 :w 新文件 保存到新文件 类似另存为,新文件存在,报错 :w! 新文件 保存到新文件,新文件存在,覆盖 : ...