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( ...
随机推荐
- 切身体验苹果Reminders的贴心设计
今天吃晚饭时在iPhone的Reminders上添加了一个任务并且设定了时间. 回来后忘了这个任务,在iPad上看优酷视频时,iPad上的Reminders突然跳出提示框,优酷视频随之暂停. MacB ...
- perl在命令行中打印单引号
perl -e 'print "\'";' 这样写是不行的,这里直接执行会当作这命令未结束.反斜杠对符号做了转义,这样的命令解释成: perl -e 'print ';' (双 ...
- 架构模式对象与关系结构模式之:标识域(Identity Field)
一:标识域(Identity Field) 标识域(Identity Field)可以理解为主键.使用领域模型和行数据入口的时候,就要使用标识域,因为这两个对象代表的是唯一存在的那个数据记录.事务脚本 ...
- [C++] socket - 4 [线程同步 简单例子]
/*WINAPI 线程同步*/ #include<windows.h> #include<stdio.h> DWORD WINAPI myfun1(LPVOID lpParam ...
- 原生JavaScript事件详解
JQuery这种Write Less Do More的框架,用多了难免会对原生js眼高手低. 小菜其实不想写这篇博客,貌似很初级的样子,但是看到网络上连原生js事件绑定和解除都说不明白,还是决定科普一 ...
- jenkins2 Jenkinsfile和load
更复杂的pipeline实例,使用了Jenkinsfile和load. 文章来自:http://www.ciandcd.com 文中的代码来自可以从github下载: https://github.c ...
- iOS YSDropdownMagnify 下拉放大,向上导航显示
要实现的效果如上.在实际开发中,我们会使用到三种方式来实现. 通过隐藏导航栏,自定义导航View 改变原生导航栏背景透明 原生导航栏通过添加背景图片改变 个人是比较喜欢第二种. github下载地址: ...
- paip.提升性能3倍--使用栈跟VirtualAlloc代替堆的使用.
paip.提升性能3倍--使用栈跟VirtualAlloc代替堆的使用. #----为什么要设计堆栈,它有什么独特的用途? 为了性能 .... 堆比栈的性能 也有的说法为了编程容易...这个是错误的 ...
- UC脱茧蜕变,移动资讯市场格局再生变
日前,UC浏览器正式更名为UC,同时正式发布大数据驱动的独立资讯应用“UC头条”.而整个UC品牌也从工具类升级为优质资讯内容平台,并吹响了向“大数据新型媒体平台”进军的冲锋号.根据UC官方公布的数据显 ...
- 地图源改变之后mxd文件打开很慢的问题
在使用ArcGIS开发电子地图程序时,有时候需要更换服务器地址,这时打开MXD文件就会非常慢,一直没有找到有效的方法,下面是从网上搜到的方法,还没有验证,下次再碰到这个问题的时候,验证一下: (以下方 ...