ci配置smarty手记
需要用ci来写一个后台配置smarty,在网络上能够找到一些相关的文章.但是都是比较旧的内容,大部分是smary2.*
的配置方法.按照这个配置后会出现一些错误.其实配置看smary官方会比较简单.
基础
在php中使用smarty的用法
require_once('Smarty.class.php');
$smarty = new Smarty();
这里就可以使用对象$smarty
的assign和display对象来解析模板.在ci里面使用时为了在controller里面来使用这两个函数.
配置
smarty里面有4个需要配置的项
$smarty->setTemplateDir( ...);
$smarty->setCompileDir(... );
$smarty->setConfigDir( ...);
$smarty->setCacheDir(... );
那么我们在ci的config里面创建一个smarty.php的文件,并加入4个变量.其中APPPATH
的值为application目录.创建'templates_c',其他三个文件夹ci里面都存在.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['template_dir'] = APPPATH . 'views';
$config['compile_dir'] = APPPATH . 'templates_c';
$config['cache_dir'] = APPPATH . 'cache';
$config['config_dir'] = APPPATH . 'config';
类库
首先将smarty的lib目录复制到ci的libraries目录,并改名为smarty.在libraries里面创建一个ci_smarty.php
的文件.这里主要是加载配置文件等.
<?php
if(!defined('BASEPATH')) EXIT('No direct script asscess allowed');
require_once( APPPATH . 'libraries/smarty/Smarty.class.php' );
class Ci_smarty extends Smarty {
protected $ci;
public function __construct(){
parent::__construct();
$this->ci = & get_instance();
$this->ci->load->config('smarty');//加载smarty的配置文件
//获取相关的配置项
// $this->template_dir= .. ;这是2.*的方法,3.1之后修改为 getXXX setXXX
$this->setTemplateDir($this->ci->config->item('template_dir'));
$this->setCompileDir($this->ci->config->item('compile_dir'));
$this->setCacheDir($this->ci->config->item('cache_dir'));
$this->setConfigDir($this->ci->config->item('config_dir'));
}
}
然后在config/autoload.php里面设置自动加载Ci_smarty
$autoload['libraries'] = array('ci_smarty');
自定义控制器
在core
文件夹添加一个My_Controller.php
的自定义控制器.将smarty的assign和display两个函数添加进入.
<?php if(!defined('BASEPATH')) EXIT('No direct script asscess allowed');
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function assign($key,$val) {
$this->ci_smarty->assign($key,$val);
}
public function display($html) {
$this->ci_smarty->display($html);
}
}
将控制器继承自My_Controller就可以使用这两个函数了.
实例
控制器继承需要修改为My_Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends My_Controller {
public function index()
{
//$this->load->view('welcome_message');
$data["title"]="标题";
$data["num"]="123123";
$this->assign('data',$data);
$this->display("index.html");
}
}
view文件夹中的index.html文件
<html>
<head>
<title>{$data.title}</title>
</head>
<body>
<p>{$data.num}</p>
</body>
</html>
ci配置smarty手记的更多相关文章
- 转:CI配置SMARTY
1.到相应站点下载Smarty的源码包:2.将源码包里面的libs文件夹copy到CI的项目目录下面的libraries文件夹下,并重命名为Smarty:3.在目录 application/libra ...
- CI集成Smarty的实现方式
给新伙伴的忠告:不要去想着有多复杂,看一遍绝对就会弄了! 这样集成的目的是什么? 因为我使用过CI和smarty,所以我就按自己的理解讲一下:CI框架在控制器.models方面做的很好,但在多变的视图 ...
- 配置SMarty解析
在 common/main.php中配置 View 组件 'view' => [ 'renderers' => [ 'tpl' => [ 'class' => 'yii\sma ...
- PHP中如何配置smarty框架实现PHP代码和HTML代码分离
header('Cache-Control:Private');//保留用户填写的信息 session_start();//开启缓存 define('MYCMS','UTF-8');//定义网站编码常 ...
- CI整合Smarty
1.到相应的站点下载smarty模板: 2.将源代码中的libs目录复制到项目的libraries目录下,改名为smarty3.0 3.在项目目录的libraries文件夹内新建文件ci_smarty ...
- 如何配置Smarty模板
<?php //首先包含Smarty类文件 include_once('Smarty/Smarty.class.php'); //实例化Smarty类文件 $smarty=new Smarty( ...
- Nginx.PHP配置Smarty
下载http://smarty.net: 解压 -> 将 libs 文件夹重命名 smartyLibs -> 放置在自己服务器的 usr/local/lib/ 中 (/usr/local/ ...
- SpringBoot项目的CI配置 # 安全变量
运行GitLab Runner容器 参考Run GitLab Runner in a container - Docker image installation and configuration 执 ...
- CI 配置验证规则
//判断表单域,提交表单显示对应的错误信息 $this->load->library('form_validation'); $config = array( ...
随机推荐
- redis和ssdb读取性能对比
最近关注了一下ssdb,它的特点是基于文件存储系统所以它支撑量大的数据而不因为内存的限制受取约束.从官网的测试报告来看其性能也非常出色和redis相当,因此可以使用它来代替redis来进行k-v数据业 ...
- C#设计模式——单件模式
一.为何需要单件模式 需求 我们开发了一个大型的项目,其中存在许多的工具类.但是其中很多的工具类我们并不是经常使用得到,甚至 一次都不会使用.但是这些工具类都是静态的类,会消耗很多的内存,即使一次都不 ...
- 【Leetcode】【Medium】Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 解题思路: ...
- MVC4+WebApi+Redis Session共享练习(上)
这几天生病了,也没有心情写博客,北京医院真心伤不起呀,钱不少花,病没治好,还增加了新病,哎不说了,周末还得去大医院检查一下,趁女盆友还没有回来,把前几天写的东西总结一下.本文也会接触一点webApi的 ...
- Homebrew- MAC上的包管理利器
包管理器是神马东西?让我们看看wikipedia上的介绍. In software, a package management system, also called package manager, ...
- 为什么Web 设计会‘死’?
高质量的Web 模板,成熟的Design Pattern,人工智能的引用,移动技术的冲击是否标志着Web Design 结束的时代已经到来? Web Design 最终也未避免与“死亡”这个词的关联, ...
- 浅谈Entity Framework中的数据加载方式
如果你还没有接触过或者根本不了解什么是Entity Framework,那么请看这里http://www.entityframeworktutorial.net/EntityFramework-Arc ...
- C++ 泛型算法
<C++ Primer 4th>读书笔记 标准容器(the standard container)定义了很少的操作.标准库并没有为每种容器类型都定义实现这些操作的成员函数,而是定义了一组泛 ...
- 往linux上传、下载
http://skypegnu1.blog.51cto.com/8991766/1538371
- 备份MYSQL出现:mysqldump: Got error: 1049: Unknown database 'test 'when selecting the data
解决办法 后面不要加分号: 如: mysqldump -uroot -p test>test.sql 不加分号 直接回车