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. 【MINA】OrderedThreadPoolExecutor和UnorderedThreadPoolExecutor的事件监听线程池的选择

    mina中有两个线程池概念 1.处理监听建立连接的线程池  2.处理读写事件的线程池 本文中主要探讨读写事件的线程池的选择 这两种都经过实际项目的使用和检测,说说优缺点 早期的项目是用Unordere ...

  2. 阻止子View获取焦点方法

    android:descendantFocusability:ViewGroup ep: android:descendantFocusability=blocksDescendants

  3. angularjs开发总结

    使用AngularJS有差不多一年时间了,前前后后也用了不少库和指令,整理了一下,分成四大类列出.有demo地址的,就直接连接到demo地址,其它的直接链到github托管库中. 图片视频类 angu ...

  4. solr-tomcat 中文乱码

    <Connector URIEncoding="UTF-8" port="8080" protocol="HTTP/1.1" conn ...

  5. ios fix UIRefreshControl bug

    NS_CLASS_AVAILABLE_IOS(6_0)   UIRefreshControl 有个毛病有时会出bug  动画下拉就不动了,这里给出修复处理: @interface UICollecti ...

  6. NSdata 与 NSString,Byte数组,UIImage 的相互转换

    1. NSData 与 NSString NSData-> NSString NSString *aString = [[NSString alloc] initWithData:adataen ...

  7. Swift轻松入门——基本语法介绍和详细地Demo讲解(利用WebView打开百度、新浪等网页)

    转载请务必注明出处(all copyright reserved by iOSGeek) 本文主要分为两个部分,第一部分介绍Swift的基本语法,第二部分讲解一个利用WebView来打开百度.sina ...

  8. HDOJ 1024 Max Sum Plus Plus -- 动态规划

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Problem Description Now I think you have got an ...

  9. ubuntu下编译安装PHP

    首先配置configure // ./configure --prefix=/usr/local/php5 --with-apxs2=/usr/local/apache2/bin/apxs --wit ...

  10. appcan 跨窗口处理方法 appcan.window.evaluateScript({name,scriptContent,type})使用解读

    appcan.window.evaluateScript({ name,/*主窗口名称,此窗口要先用appcan.window.open打开了,才能找到,此方法才会有效*/ scriptContent ...