需要用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. (转)对.net系统架构改造的一点经验和教训

    在互联网行业,基于Unix/Linux的网站系统架构毫无疑问是当今主流的架构解决方案,这不仅仅是因为Linux本身足够的开放性,更因为围绕传统Unix/Linux社区有大量的成熟开源解决方案,覆盖了网 ...

  2. linux下vi操作出现E325: ATTENTION的解决方法

    #rm  ***.***.swp #rm: remove regular file `.Test.java.swp'? y # OK,完成.再用VI打开就可以了.

  3. 怎么实现元素ol的降序排序显示

    首先介绍一下什么是ol元素.这里直接引用MDN里面的定义:The HTML <ol> Element (or HTML Ordered List Element) represents a ...

  4. maven 打包 xml文件

    说起来手贱啊,搞了两个小时,就是因为打包的时候,maven无法把xml文件打包到正确的位置. 本来应该是打包到 com/presistence包下,结果打出来有两个包,一个是com/presisten ...

  5. 初学者利用git 上传代码到Coding的简单操作步骤

    1.首先登陆coding网站注册账号https://coding.net/ (注册完后需登陆邮箱激活邮件) 2.登陆刚注册的coding账号 ,添加项目 添加项目—〉输入项目名称—〉输入对项目的简单描 ...

  6. .NET面试题大全,包括微软、华为、中兴等大企业的面试真题

    简要地写出一个.NET Remoting的示例 [考点].NET Remoting程序的原理,.NET Remoting的基本编写方法.[出现频率]★★☆☆☆[解答]在VS 2008中添加新的类库项目 ...

  7. Redis教程(七):Key操作命令详解

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/134.html?1455807040 一.概述: 在该系列的前几篇博客中, ...

  8. 【转】关于KDD Cup '99 数据集的警告,希望从事相关工作的伙伴注意

    Features From: Terry Brugger Date: 15 Sep 2007 Subject: KDD Cup '99 dataset (Network Intrusion) cons ...

  9. Web调试工具——Fiddler介绍

    Fiddler 教程 Fiddler是最强大最好用的Web调试工具之一,它能记录所有客户端和服务器的http和https请求,允许你监视,设置断点,甚至修改输入输出数据. 使用Fiddler无论对开发 ...

  10. 博客还是搬到csdn吧~

    从csdn转到博客园是因为博客园开放了博客接口,方便发博客,再者博客样式也比较多,排版兼容性也较好. but, 博客园的链接在多个群里进行了宣传,当时大概有2000多的访问量,但是一个月过去了,现在还 ...