Rumah > pembangunan bahagian belakang > tutorial php > CI框架中使用通用模板引擎smarty

CI框架中使用通用模板引擎smarty

PHP中文网
Lepaskan: 2023-03-15 15:20:02
asal
2365 orang telah melayarinya

 CI版本:2.1.4 // 此时的最新版本
Smarty版本:Smarty-2.6.26 // 因为我之前用这个版本,为了照顾自己的使用习惯,这里没有使用最新的Smaty版本,大家理解了扩展原理,可以选择自己想用的Smatry版本。


1、到相应站点下载Smarty的源码包; // 我这里用的是 Smarty-2.6.26
2、将源码包里面的libs文件夹copy到CI的项目目录下面的libraries文件夹下,并重命名为Smarty-2.6.26;//
3、在项目目录的libraries文件夹内新建文件Cismarty.php,里面的内容如下:

<?php 
if(!defined(&#39;BASEPATH&#39;)) EXIT(&#39;No direct script asscess allowed&#39;); 
require_once( APPPATH . &#39;libraries/Smarty-2.6.26/libs/Smarty.class.php&#39; ); 
class Cismarty extends Smarty { 
    protected $ci; 
    public function  __construct(){ 
        $this->ci = & get_instance(); 
        $this->ci->load->config(&#39;smarty&#39;);//加载smarty的配置文件 
        //获取相关的配置项 
        $this->template_dir   = $this->ci->config->item(&#39;template_dir&#39;); 
        $this->complie_dir    = $this->ci->config->item(&#39;compile_dir&#39;); 
        $this->cache_dir      = $this->ci->config->item(&#39;cache_dir&#39;); 
        $this->config_dir     = $this->ci->config->item(&#39;config_dir&#39;); 
        $this->template_ext   = $this->ci->config->item(&#39;template_ext&#39;); 
        $this->caching        = $this->ci->config->item(&#39;caching&#39;); 
        $this->cache_lifetime = $this->ci->config->item(&#39;lefttime&#39;); 
    } 
}
Salin selepas log masuk

4、在项目目录的config文件夹内新建文件smarty.php文件,里面的内容如下:

<?php  if ( ! defined(&#39;BASEPATH&#39;)) exit(&#39;No direct script access allowed&#39;); 
$config[&#39;theme&#39;]        = &#39;default&#39;; 
$config[&#39;template_dir&#39;] = APPPATH . &#39;views&#39;; 
$config[&#39;compile_dir&#39;]  = FCPATH . &#39;templates_c&#39;; 
$config[&#39;cache_dir&#39;]    = FCPATH . &#39;cache&#39;; 
$config[&#39;config_dir&#39;]   = FCPATH . &#39;configs&#39;; 
$config[&#39;template_ext&#39;] = &#39;.html&#39;; 
$config[&#39;caching&#39;]      = false; 
$config[&#39;lefttime&#39;]     = 60;
Salin selepas log masuk

5、在入口文件所在目录新建文件夹templates_c、cache、configs;
6、在项目目录下面的config目录中找到autoload.php文件
修改这项

$autoload['libraries'] = array('Cismarty');//目的是:让系统运行时,自动加载,不用认为的在控制器中手动加载


7、在项目目录的core文件夹中新建文件MY_Controller.php 内容如下: // 扩展核心控制类

<?php if (!defined(&#39;BASEPATH&#39;)) exit(&#39;No direct access allowed.&#39;); 
class MY_Controller extends CI_Controller { // 原文这里写错 
    public function __construct() { 
        parent::__construct(); 
    } 
    public function assign($key,$val) { 
        $this->cismarty->assign($key,$val); 
    } 
    public function display($html) { 
        $this->cismarty->display($html); 
    } 
}
Salin selepas log masuk

配置完毕


------------------------------------------------------------------------------------------------------------------------------------------------------
使用方法实例:
在控制器中如:

<?php if ( ! defined(&#39;BASEPATH&#39;)) exit(&#39;No direct script access allowed&#39;); 
class Welcome extends MY_Controller { // 原文这里写错 
    public function index() 
    { 
        //$this->load->view(&#39;welcome_message&#39;); 
        $data[&#39;title&#39;] = &#39;标题&#39;; 
        $data[&#39;num&#39;] = &#39;123456789&#39;; 
        //$this->cismarty->assign(&#39;data&#39;,$data); // 亦可 
        $this->assign(&#39;data&#39;,$data); 
        $this->assign(&#39;tmp&#39;,&#39;hello&#39;); 
        //$this->cismarty->display(&#39;test.html&#39;); // 亦可 
        $this->display(&#39;test.html&#39;); 
    } 
}
Salin selepas log masuk

然后再视图中:试图文件夹位于项目目录的views之下:
新建文件test.html

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>{ $test.title}</title> // 原文是 <title>{$test[&#39;title&#39;]}</title>,是错误的写法,也有可能是Smarty版本的原因 
<style type="text/css"> 
</style> 
</head> 
<body> 
{$test.num|md5} // 原文这里也写错了 
<br> 
{$tmp} 
</body> 
</html>
Salin selepas log masuk

本文地址:http://www.php.cn/php-weizijiaocheng-377484.html

学编程就来 PHP中文网   www.php.cn

Atas ialah kandungan terperinci CI框架中使用通用模板引擎smarty. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan