下载CodeIgniter并解压,然后部署服务器(例如使用Apache,可自行添加vhost,设定路径和端口号)。
CI2下载地址:http://ellislab.com/codeigniter
下载Smarty3:http://www.smarty.net/download
解压后,修改最外层根目录文件夹名称为smarty, 然后拷贝挣个smarty文件夹到CI的application/libraries/文件夹下。
在CI的application/libraries/文件夹下(此时这个文件夹下应该还有刚拷贝进来的smarty程序),新建smarty.php文件,内容如下:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH.'libraries/smarty/libs/Smarty.class.php');
class CI_Smarty extends Smarty {
function __construct()
{
parent::__construct();
$this->setTemplateDir(APPPATH.'views');
$this->setCompileDir(APPPATH.'views/compiled');
$this->setConfigDir(APPPATH.'libraries/smarty/configs');
$this->setCacheDir(APPPATH.'libraries/smarty/cache');
$this->assign('APPPATH', APPPATH);
$this->assign('BASEPATH', BASEPATH);
if ( method_exists($this, 'assignByRef')) {
$ci =& get_instance();
$this->assignByRef("ci", $ci);
}
$this->force_compile = 1;
$this->caching = true;
$this->cache_lifetime = 120;
}
function view($template_name) {
if (strpos($template_name, '.') === FALSE && strpos($template_name, ':') === FALSE) {
$template_name .= '.php';
}
parent::display($template_name);
}
}
修改application/config/目录下的autoload.php文件,在$autoload['libraries']里添加smarty,例如:
$autoload['libraries'] = array('smarty', 'database');
新建controller文件test.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Test extends CI_Controller {
public function index() {
$this->smarty->assign("title","Testing Smarty");
$this->smarty->assign("description", "This is the testing page for integrating Smarty and CodeIgniter.");
$this->smarty->view('test');
}
}
还有view文件:test.php
<p>test.php page shows the variables here:</p>
<p>title: {$title}</p>
<p>description: {$description}</p>
注意,需要在CI的application/views/目录下新建compiled文件夹(因为在上面的smarty.php中已经设定了将在views/compiled下存在smarty编译后的php文件)
这样通过URL正常访问这个test controller应该能看这个通过smarty模板技术生成的view了。
320

被折叠的 条评论
为什么被折叠?



