![](http://codeigniter.org.cn/forums/static/image/filetype/zip.gif)
【此步骤经过测试直接放在项目目录里面的也是可以的 比如:application/libraries/smarty里面然后第五步骤写一个类库,然后自动加载这个文件】
system/libs/smarty
然后在将smarty的类库文件放入到上面的smarty目录下。 第五步:将codeigniter-smarty-3.zip包里的文件直接拷贝到根目录下的对应文件夹中。然后创建Smarty 的类库文件Smarty.php。代码如下:
PHP 复制代码
<?php if (!('BASEPATH')) ('No direct script access allowed'); /** * Smarty Class * * @package CodeIgniter * @subpackage Libraries * @category Smarty * @author Kepler Gelotte * @link */ require_once( BASEPATH.'libs/smarty/libs/Smarty.class.php' ); class CI_Smarty extends Smarty { function CI_Smarty(){ parent::Smarty(); $this->compile_dir = APPPATH . "views/templates_c"; $this->template_dir = APPPATH . "views/templates"; $this->assign( 'APPPATH', APPPATH ); $this->assign( 'BASEPATH', BASEPATH ); log_message('debug', "Smarty Class Initialized"); } function __construct(){ parent::__construct(); $this->compile_dir = APPPATH . "views/templates_c"; $this->template_dir = APPPATH . "views/templates"; $this->assign( 'APPPATH', APPPATH ); $this->assign( 'BASEPATH', BASEPATH ); // Assign CodeIgniter object by reference to CI if ( ( $this, 'assignByRef') ){ $ci =& get_instance(); $this->assignByRef("ci", $ci); } log_message('debug', "Smarty Class Initialized"); } /** * Parse a template using the Smarty engine * This is a convenience method that combines assign() and * display() into one step. * * Values to assign are passed in an associative array of * name => value pairs. * If the output is to be returned as a string to the caller * instead of being output, pass true as the third parameter. * * @access public * @param string * @param array * @param bool * @return string */ function view($template, $data = (), $return = FALSE){ foreach ($data as $key => $val){ $this->assign($key, $val); } if ($return == FALSE){ $CI =& get_instance(); $CI->output->final_output = $this->fetch($template); return; }else{ return $this->fetch($template); } } } // END Smarty Class
然后再将文件保存到下面的目录下:
application/libraries/Smarty.php
原文中放置的目录是 /system/application/libraries/Smarty.php 但是我这边就报错。 第六步:为让CI每次自动载入Smarty库。打开/system/application/config/autoload.php文件,在$autoload['libraries']中添加smarty的库名如下:
PHP 复制代码
$autoload['libraries'] = ('smarty');
最后呢,你就可以试试看SMARTY库时候正常工作了。在前面的那个压缩包内已经存放有原作者的测试文件。测试路径为: http://your-site/index.php/example 正确的效果页如下截图: 更新说明:大家在配置好Smarty环境后经常会报下面这个错误!
出现这个错误的时候大家不要急,直接打开libs/sysplugins/smarty_internal_data.php文件,然后查找$_variable这个变量的名称。出错的地方多半是写成了$$_variable了。删除前面多的$符号即可!