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. Activiti源码浅析:Activiti的活动授权机制

    1. IdentityLink与TaskEntity An identity link is used to associate a task with a certain identity. For ...

  2. [转]在SQLServer中实现Sequence的高效方法

    如果在ORACLE里面用惯了Sequence的兄弟们,要在SqlServer里实现Sequence,就会发现没有现成的Sequence对象可以Create了.那应该怎么办呢? 当然这点小问题是难不倒我 ...

  3. springmvc前后端传值总结

    1      前端向后端传参 1.1    普通方式传参 1.1.1         页面 参数需要解析成json对象:JSON.parse(JSON.stringify(query)) $.getJ ...

  4. 敌情篇 ——DDoS攻击原理

    敌情篇 ——DDoS攻击原理 DDoS攻击基础 DDoS(Distributed Denial of Service,分布式拒绝服务)攻击的主要目的是让指定目标无法提供正常服务,甚至从互联网上消失,是 ...

  5. selenium简单代码入门

    #!/usr/bin/env python #-*- coding:utf-8 -*- import os,sys,string import time import unittest from se ...

  6. Xcode中添加代码块的方式

    在写代码的过程中,经常会有重复的代码(比如说,cell的使用),当然了复制粘贴也不是不行,但是Xcode提供了一个很方便的东西. 1.在Xcode右下角你会看到有一个{}的东西,这里是一些常用的代码块 ...

  7. ReactiveCocoa入门教程——第一部分(转)

    作为一个iOS开发者,你写的每一行代码几乎都是在响应某个事件,例如按钮的点击,收到网络消息,属性的变化(通过KVO)或者用户位置的变化(通过CoreLocation).但是这些事件都用不同的方式来处理 ...

  8. MongoDB使用记录

    安装服务 使用以下命令将MongoDB安装成为Windows服务.笔者的MongoDB目录为D:\Program Files\mongodb mongod --logpath "D:\Pro ...

  9. HTML Music Entities/音乐符号

    HTML Music Entities Musical symbols Description Character(click) HTML-Entity Code-Decimal Code-Hex Q ...

  10. JDK1.8 HashMap中put源码分析

    一.存储结构      在JDK1.8之前,HashMap采用桶+链表实现,本质就是采用数组+单向链表组合型的数据结构.它之所以有相当快的查询速度主要是因为它是通过计算散列码来决定存储的位置.Hash ...