Ci框架整合smarty模板引擎
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模板引擎的更多相关文章
- ThinkPHP3.2.3整合smarty模板(二)
前言:继ThinkPHP3.2.3整合smarty模板(一)之后,继续来探讨一下tp框架整合smarty模板,看到有人在群上问到怎么使用自定义的常量,今天就具体来谈谈: 一.开发一个项目,必不可少会用 ...
- ci框架与smarty的整合
ci框架与smarty的整合 来源:未知 时间:2014-10-20 11:38 阅读数:108 作者:xbdadmin [导读] Ci 和 smarty 的完美结合 Ci 结合 sma ...
- 深入浅出之Smarty模板引擎工作机制(一)
深入浅出Smarty模板引擎工作机制,我们将对比使用smarty模板引擎和没使用smarty模板引擎的两种开发方式的区别,并动手开发一个自己的模板引擎,以便加深对smarty模板引擎工作机制的理解. ...
- smarty模板引擎
1. 使用smarty 1.1 项目引入 // 3, 连接数据库,提取相关数据 $title = "Smarty模板引擎"; $content = "Smarty模 ...
- 写一个迷你版Smarty模板引擎,对认识模板引擎原理非常好(附代码)
前些时间在看创智博客韩顺平的Smarty模板引擎教程,再结合自己跟李炎恢第二季开发中CMS系统写的tpl模板引擎.今天就写一个迷你版的Smarty引擎,虽然说我并没有深入分析过Smarty的源码,但是 ...
- 深入浅出之Smarty模板引擎工作机制(二)
源代码下载地址:深入浅出之Smarty模板引擎工作机制 接下来根据以下的Smarty模板引擎原理流程图开发一个自己的模板引擎用于学习,以便加深理解. Smarty模板引擎的原理,其实是这么一个过程: ...
- Smarty模板引擎技术二
Smarty模板引擎技术 内建函数 include_php内建函数 作用:载入一个php文件,将载入的文件的内容赋值给一个变量 注意:该内建函数只能在2.0中使用,如果使用的话,必须得实例化Sma ...
- Smarty模板引擎技术
Smarty模板引擎技术 什么是模板引擎? 什么是Smarty模板引擎? 为何选择Smarty模板引擎? 如何使用Smarty模板引擎? 一.历史背景 场景一:回顾之前编写PHP项目的方式 //链接数 ...
- 迷你版 smarty --模板引擎和解析
http://blog.ipodmp.com/archives/php-write-a-mini-smarty-template-engine/ 迷你版Smarty模板引擎目录结构如下: ① 要开发一 ...
随机推荐
- 2015年校园招聘12家IT公司面试体验
背景 2015年注定是一个不平凡的年头,作为一个应届毕业生,我也算是经历了工作上的大起大落.下面我先简单讲述一下自己的遭遇,然后根据自己亲身的面试经历,从一个学生的角度去谈谈自己对面试过的公司的一些看 ...
- Android Studio依赖dependencies和Eclipse加上lib包解决重复编译某些项目的问题
android运行时编译,可以在android的dependencies里面加语句, 一般是compile 'com.android.support:appcompat-v7:22.2.1' comp ...
- SQL数据库开发知识总结:提高篇
1.联合结果集 (1) 简单的结果集联合 select FNumber,FName,Fage from T_Employee\ union select FidCardNumber,FName,Fag ...
- 设计模式------PROTOTYPE(原型),TEMPLATE(模板)
看链接:http://blog.csdn.net/wuzhekai1985/article/details/6667020.纯属为自己学习所使用. 对于原型模式的理解:就如连接中所说,制作简历时先手写 ...
- 如何给windows窗体程序打包成一个安装包
http://blog.csdn.net/xyy410874116/article/details/6341787 给windows窗体程序打包成一个安装包:具体操作在:http://hi.baidu ...
- C# 编码约定
参考自 MSDN https://msdn.microsoft.com/zh-cn/library/ff926074.aspx , 只摘要个人觉得有用部分 命名约定 在不包括 using 指令 ...
- 谱曲软件-MuseScore
谱曲软件-MuseScore 参考: 1.MuseScore官网 2.免费乐谱制作软体MuseScore
- 学习C++ Primer 的个人理解(二)
本身就一定基础的读者我想变量常量这些概念应该已经不是问题了.但是本章还是有几个重点,需要特别留意一下的: 1.初始化和赋值是不同的操作 2.任何非0值都是true 3.使用新标准列表初始化,在有丢失精 ...
- Wix: Using Patch Creation Properties - Small Update
Source Reference: wix help document -- WiX Toolset License Using Patch Creation Properties A patch ...
- jtemplate使用笔记
最近的项目中用到了jtemplate, 它是客户端基于javascript的模板引擎,绑定的数据为json对象.以前我在页面上显示数据列表时最喜欢用Repeater控件了,因为它相对与其它几个服务端控 ...