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. java程序执行顺序

    原来自己一直都没弄明白Java程序的执行顺序问题,今天,自己写了个测试,果然与自己考虑的有差距 测试代码: 一个父类Animal 一个子类Dog 测试类Test 运行结果: 所以执行顺序是: 父类An ...

  2. nyist 42 一笔画 (欧拉回路 + 并查集)

    nyoj42 分析: 若图G中存在这样一条路径,使得它恰通过G中每条边一次,则称该路径为欧拉路径. 若该路径是一个圈,则称为欧拉(Euler)回路. 具有欧拉回路的图称为欧拉图(简称E图).具有欧拉路 ...

  3. Java Thread interrupt

    现有线程对象threadA,调用threadA.interrupt(),则threadA中interrupted状态会被置成false,很多线程中都是通过isInterrupted()方法来检测线程是 ...

  4. java中IO流的操作

    读取转换流--读取键盘录入中键盘录入一行数据并打印其大写,发现就是读一行数据的原理.也就是readLine方法.能不能直接使用readLine方法来完成键盘录入一行数据的读取呢?readLine方法是 ...

  5. 再议Unity 3D

    一年前,偶发冲动,翻译了<[译] Unity3D游戏和facebook绑定(1:简介)>系列文章. 现在看有2个明显的好处, 一:给这个不温不火的博客带了top 3的人气: 二:我个人由此 ...

  6. 03静态链表_StaticLinkList--(线性表)

    #include "string.h" #include "ctype.h" #include "stdio.h" #include &qu ...

  7. Java线程间通信--生产者消费者

    class ProducerConsumerDemo {    public static void main(String[] args)     {        Resource r = new ...

  8. 常用Linux/Unix/Mac Os命令

    常用Linux/Unix/Mac OS命令 参考: 1.50 Most Frequently Used UNIX / Linux Commands (With Examples)

  9. Wix: Using Patch Creation Properties - Small Update

    Source Reference: wix help document  -- WiX Toolset License Using Patch Creation Properties  A patch ...

  10. jQuery实例-简单选项卡-【一些常见方法(2)-练习】

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...