需要用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手记的更多相关文章

  1. 转:CI配置SMARTY

    1.到相应站点下载Smarty的源码包:2.将源码包里面的libs文件夹copy到CI的项目目录下面的libraries文件夹下,并重命名为Smarty:3.在目录 application/libra ...

  2. CI集成Smarty的实现方式

    给新伙伴的忠告:不要去想着有多复杂,看一遍绝对就会弄了! 这样集成的目的是什么? 因为我使用过CI和smarty,所以我就按自己的理解讲一下:CI框架在控制器.models方面做的很好,但在多变的视图 ...

  3. 配置SMarty解析

    在 common/main.php中配置 View 组件 'view' => [ 'renderers' => [ 'tpl' => [ 'class' => 'yii\sma ...

  4. PHP中如何配置smarty框架实现PHP代码和HTML代码分离

    header('Cache-Control:Private');//保留用户填写的信息 session_start();//开启缓存 define('MYCMS','UTF-8');//定义网站编码常 ...

  5. CI整合Smarty

    1.到相应的站点下载smarty模板: 2.将源代码中的libs目录复制到项目的libraries目录下,改名为smarty3.0 3.在项目目录的libraries文件夹内新建文件ci_smarty ...

  6. 如何配置Smarty模板

    <?php //首先包含Smarty类文件 include_once('Smarty/Smarty.class.php'); //实例化Smarty类文件 $smarty=new Smarty( ...

  7. Nginx.PHP配置Smarty

    下载http://smarty.net: 解压 -> 将 libs 文件夹重命名 smartyLibs -> 放置在自己服务器的 usr/local/lib/ 中 (/usr/local/ ...

  8. SpringBoot项目的CI配置 # 安全变量

    运行GitLab Runner容器 参考Run GitLab Runner in a container - Docker image installation and configuration 执 ...

  9. CI 配置验证规则

    //判断表单域,提交表单显示对应的错误信息      $this->load->library('form_validation');      $config = array(      ...

随机推荐

  1. QSS总结以及最近做的Qt项目

    什么是QSS QSS称为Qt Style Sheets也就是Qt样式表,它是Qt提供的一种用来自定义控件外观的机制.QSS大量参考了CSS的内容,只不过QSS的功能比CSS要弱很多,体现在选择器要少, ...

  2. [计算机图形学] 基于C#窗口的Bresenham直线扫描算法、种子填充法、扫描线填充法模拟软件设计(二)

    上一节链接:http://www.cnblogs.com/zjutlitao/p/4116783.html 前言: 在上一节中我们已经大致介绍了该软件的是什么.可以干什么以及界面的大致样子.此外还详细 ...

  3. [数据库操作]Java中的JDBC的使用方法.

    前言:想必大家在实际编码中都遇到过JDBC的操作, 这里仅做自己的一个总结, 有错误和不完整之处还请大家提出来. 1,JDBC其实一套规范(接口)数据库厂商需要实现此接口(实现类)--数据库驱动 2, ...

  4. AT&T Assembly for Linux and Mac (sys_write)

    Write() in C : (sys_write.c) #include <stdio.h> int main(void) { printf("Hello Landpack\n ...

  5. paip.php 5.0 5.3 5.4 5.5 -6.0的新特性总结与比较

    paip.php 5.0 5.3 5.4  5.5 -6.0的新特性总结与比较 PHP5的新特性 2 · 对象的参照过渡是默认的(default) 3 · 引入访问属性的限制 3 · 引入访问方法的限 ...

  6. 看2015年TFC游戏大会,云计算何以唱主角

    日前,第10界TFC游戏大会浩浩荡荡地在北京国际会议中心成功举办了.与往届不同的是,这一次TFC的金苹果奖被四家云计算公司夺走,它们分别是金山云.阿里云.腾讯云和首都在线.四家云计算公司夺走了游戏大会 ...

  7. iOS开发——高级技术&iCloud服务

    iCloud服务 iCloud 是苹果提供的云端服务,用户可以将通讯录.备忘录.邮件.照片.音乐.视频等备份到云服务器并在各个苹果设备间直接进行共享而无需关心数据同步问题,甚至 即使你的设备丢失后在一 ...

  8. 修改JSONArray里所有key的值

    下面举一个代码的列子目的是实现如下功能: [{"userId":1,"userName":"plf"},{"userId" ...

  9. Help Viewer 2.2 独立运行

    "C:\Program Files (x86)\Microsoft Help Viewer\v2.2\HlpViewer.exe" /catalogName VisualStudi ...

  10. FindFriendsServer服务搭建

    本文介绍如何搭建FindFriendsServer(https://github.com/hnrainll/FindFriendsServer)所需的环境. 环境需要: Windows+Apache+ ...