日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

php ci框架 模板输出,CI框架中使用通用模板引擎smarty

發(fā)布時(shí)間:2025/4/5 php 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php ci框架 模板输出,CI框架中使用通用模板引擎smarty 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

CI版本:2.1.4 // 此時(shí)的最新版本

Smarty版本:Smarty-2.6.26 // 因?yàn)槲抑坝眠@個(gè)版本,為了照顧自己的使用習(xí)慣,這里沒(méi)有使用最新的Smaty版本,大家理解了擴(kuò)展原理,可以選擇自己想用的Smatry版本。

1、到相應(yīng)站點(diǎn)下載Smarty的源碼包; // 我這里用的是 Smarty-2.6.26

2、將源碼包里面的libs文件夾copy到CI的項(xiàng)目目錄下面的libraries文件夾下,并重命名為Smarty-2.6.26;//

3、在項(xiàng)目目錄的libraries文件夾內(nèi)新建文件Cismarty.php,里面的內(nèi)容如下:

if(!defined('BASEPATH')) EXIT('No direct script asscess allowed');

require_once( APPPATH . 'libraries/Smarty-2.6.26/libs/Smarty.class.php' );

class Cismarty extends Smarty {

protected $ci;

public function __construct(){

$this->ci = & get_instance();

$this->ci->load->config('smarty');//加載smarty的配置文件

//獲取相關(guān)的配置項(xiàng)

$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->template_ext = $this->ci->config->item('template_ext');

$this->caching = $this->ci->config->item('caching');

$this->cache_lifetime = $this->ci->config->item('lefttime');

}

}

4、在項(xiàng)目目錄的config文件夾內(nèi)新建文件smarty.php文件,里面的內(nèi)容如下:<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['theme'] = 'default';

$config['template_dir'] = APPPATH . 'views';

$config['compile_dir'] = FCPATH . 'templates_c';

$config['cache_dir'] = FCPATH . 'cache';

$config['config_dir'] = FCPATH . 'configs';

$config['template_ext'] = '.html';

$config['caching'] = false;

$config['lefttime'] = 60;

5、在入口文件所在目錄新建文件夾templates_c、cache、configs;

6、在項(xiàng)目目錄下面的config目錄中找到autoload.php文件

修改這項(xiàng)

$autoload['libraries'] = array('Cismarty');//目的是:讓系統(tǒng)運(yùn)行時(shí),自動(dòng)加載,不用認(rèn)為的在控制器中手動(dòng)加載

7、在項(xiàng)目目錄的core文件夾中新建文件MY_Controller.php 內(nèi)容如下: // 擴(kuò)展核心控制類<?php if (!defined('BASEPATH')) exit('No direct access allowed.');

class MY_Controller extends CI_Controller { // 原文這里寫錯(cuò)

public function __construct() {

parent::__construct();

}

public function assign($key,$val) {

$this->cismarty->assign($key,$val);

}

public function display($html) {

$this->cismarty->display($html);

}

}

配置完畢

------------------------------------------------------------------------------------------------------------------------------------------------------

使用方法實(shí)例:

在控制器中如:<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends MY_Controller { // 原文這里寫錯(cuò)

public function index()

{

//$this->load->view('welcome_message');

$data['title'] = '標(biāo)題';

$data['num'] = '123456789';

//$this->cismarty->assign('data',$data); // 亦可

$this->assign('data',$data);

$this->assign('tmp','hello');

//$this->cismarty->display('test.html'); // 亦可

$this->display('test.html');

}

}

然后再視圖中:試圖文件夾位于項(xiàng)目目錄的views之下:

新建文件test.html

{ $test.title} // 原文是 {$test['title']},是錯(cuò)誤的寫法,也有可能是Smarty版本的原因

{$test.num|md5} // 原文這里也寫錯(cuò)了

{$tmp}

學(xué)編程就來(lái) PHP中文網(wǎng) www.php.cn

總結(jié)

以上是生活随笔為你收集整理的php ci框架 模板输出,CI框架中使用通用模板引擎smarty的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。