在CI中要集成第三方的工具,就需要写自己的库。官方手册有详细的介绍。
要写自己的库,就需要写两个文件,一个是在application/init下面的init_myclass.php文件(如果没有init目录,自己创建)。另外一个就是在application/libraries目录下创建myclass.php文件。
这里myclass是你的类名。一些规则看手册就好了。
1)在application/libraries下创建mysmarty.php
// load Smarty library
require(’Smarty/Smarty.class.php’);
// The setup.php file is a good place to load
// required application library files, and you
// can do that right here. An example:
// require(’guestbook/guestbook.lib.php’);
class MySmarty extends Smarty {
function MySmarty()
{
// Class Constructor.
// These automatically get set with each new instance.
$this->Smarty();
$basedir=dirname(__FILE__);
$this->template_dir = “$basedir/templates/”;
$this->compile_dir = “$basedir/templates_c/”;
$this->config_dir = “$basedir/configs/”;
$this->cache_dir = “$basedir/cache/”;
//$this->compile_check = true;
//this is handy for development and debugging;never be used in a production environment.
//$smarty->force_compile=true;
$this->debugging = false;
$this->cache_lifetime=30;
$this->caching = 0; // lifetime is per cache
//$this->assign(’app_name’, ‘Guest Book’);
}
}
?>
文件路径根据具体情况修改,文件的的路径是相对你的网站的主目录开始的,而不是当前文件的当前目录,比如上面的require(’Smarty/Smarty.class.php’);不是相对application/libraries目录,而是相对$_SERVER['DOCUMENT_ROOT']目录。
2)在application/init目录下创建init_mysmarty.php。
<!--p if (!defined(’BASEPATH’)) exit(’No direct script access allowed’);<-->
if ( ! class_exists(’MySmarty’))
{
require_once(APPPATH.’libraries/mysmarty’.EXT);
}
$obj =& get_instance();
$obj->mysmarty = new MySmarty();
$obj->ci_is_loaded[] = ‘mysmarty’;
?>
3)使用
在application/controllers目录下创建一个你需要的文件,你可以这样来使用smarty
class Test extends Controller {
function Test()
{
parent::Controller();
$this->load->library(’mysmarty’);
}
function index()
{
$this->mysmarty->assign(”row”,$row);
$this->mysmarty->display(”test.tpl”);
}
}
?>
相关推荐:
[如果开发php语言的是英国人] http://justjavac.me/?p=44