CI和smarty结合一个通用方法
在坛子里看了有个人CI和smarty结合,可是还没有见到提供出来,我也说说我的方法吧。[*]先在根目录下(和index.php同等级)建立几个文件夹:tpl(放smarty模板文件),tpl_c(放smarty编译文件),
config,cache(这些文件夹的作用,请参考smarty).注:这些文件夹你可以随意放在你想要的地方,
不过我觉得方便和安全找平衡是最重要,还有就是在引入smarty时,要注意路径的设置.[*]在index.php入口文件加入一句define('ROOT', dirname(__FILE__);这样做的目的是在smarty初始化时,可以根据这个网站的
根路径来方便设置templete_dir,compile_dir等,这在下面Cismarty.php这个文件中用到.[*]再把smarty整个文件夹COPY到system/application/libraries/[*]system/application/libraries/smarty/下有Smarty.class.php等smarty的系统文件[*]在system/application/libraries/下建立一个Cismarty.php文件,这个文件是一个扩展smarty类,作用是
默认初始化smarty的一化路径配置.如templete_dir,compile_die这两个必须有配置.方便日后在controller中引入和应用,[*]在controller中使用方法:
$this->load->library('cismarty');
$this->cismarty->assign('title', $title);
……
$this->cismarty->display('xx.tpl');[*]Cismarty类的代码:[*]
<?php defined('BASEPATH') or die('Access restricted!');
/**
* 这是一个smarty的初始化类
*
* @copyright Copyright (c) 2005 - 2006 Itlong.com (www.itlong.com)
* @author 杨金焕 itlongtom@tom.com * @package Core
* @date Tue Jul 22 08:54:37 CST 2008 2008
* @version $Id$
*/
require(APPPATH.'libraries/smarty/Smarty.class.php');//APPPATH是入口文件定义的application的目录
class Cismarty extends Smarty
{
// {{{ constructor
/**
* 构造函数
*
* @access public
* @param array/string $template_dir
* @return objsmarty obj
*/
public function __construct($template_dir = '', $compile_dir = '', $config_dir = '', $cache_dir = '')
{
$this->Smarty();
if(is_array($template_dir)){
foreach ($template_dir as $key => $value) {
$this->$key = $value;
}
}
else {
//ROOT是Codeigniter在入口文件index.php定义的本web应用的根目录
//在入口文件中加入define('ROOT', dirname(__FILE__);
$this->template_dir = $template_dir ? $template_dir : ROOT . '/tpl';
$this->compile_dir= $compile_dir? $compile_dir: ROOT . '/tpl_c';
$this->config_dir = $config_dir ? $config_dir : ROOT . '/config';
$this->cache_dir = $cache_dir ? $cache_dir : ROOT . '/cache';
}
} // end func constructor
// }}}
}
/**
* 如果是在PHP5下, 我觉得,把这个类的构造函数改成一个静态static,再加一个函数,如getInstance()获得静态对象,
* 这样的单件模式,可能更适合,大家可以随意改动。
* 从这里,我们可以得到一个启发,无论整合什么模板类,编辑器,都可以用这样方法引入。
* 在坛子里,就不要再提问如何整合xajax,fck,...了。
* 有问题,可以留言QQ:23193592
*/[*]
//一个controler的例子:
<?php defined('BASEPATH') or die('Access restricted!');
/**
* 测试smarty
*
* @copyright Copyright (c) 2005 - 2006 Itlong.com (www.itlong.com)
* @author 杨金焕 itlongtom@tom.com * @package Core
* @date Tue Jul 22 09:39:35 CST 2008 2008
* @version $Id$
*/
class Demo extends Controller
{
public function Demo(){
parent::Controller();
}
public function index()
{
$this->load->helper('text');
$config = array();
$this->load->library('cismarty');
$this->cismarty->assign('title', '这是我第一个smarty在CI中的应用!');
$this->cismarty->assign('content', 'smarty和Codeigniter的结合使用:');
$code = file_get_contents(APPPATH.'libraries/Cismarty.php');
$this->cismarty->assign('code', highlight_code($code));
$this->cismarty->display('demo.tpl');
}
}[*]在ROOT/tpl/demo.tpl代码:[*]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" c>
<title>{$title}</title>
{literal}
<style type="text/css">
li{
margin:10px 0 10px 0;
}
</style>
{/literal}
</head>
<body>
<h5>Begin use the smarty view OBJ:</h5>
{$content}
<ol>
<li>先在根目录下(和index.php同等级)建立几个文件夹:tpl(放smarty模板文件),tpl_c(放smarty编译文件),<br />
config,cache(这些文件夹的作用,请参考smarty).注:这些文件夹你可以随意放在你想要的地方,<br>
不过我觉得方便和安全找平衡是最重要,还有就是在引入smarty时,要注意路径的设置.
</li>
<li>在index.php入口文件加入一句define('ROOT', dirname(__FILE__);这样做的目的是在smarty初始化时,可以根据这个网站的<br>
根路径来方便设置templete_dir,compile_dir等,这在下面Cismarty.php这个文件中用到.
</li>
<li>再把smarty整个文件夹COPY到system/application/libraries/ </li>
<li>system/application/libraries/smarty/下有Smarty.class.php等smarty的系统文件</li>
<li>在system/application/libraries/下建立一个Cismarty.php文件,这个文件是一个扩展smarty类,作用是<br />
默认初始化smarty的一化路径配置.如templete_dir,compile_die这两个必须有配置.方便日后在controller中引入和应用,<br />
</li>
<li>在controller中使用方法:<br>
$this->load->library('cismarty');<br>
$this->cismarty->assign('title', $title);<br>
……<br>
$this->cismarty->display('xx.tpl');
</li>
<li>Cismarty类的代码:<br>
<pre>
{$code}
</pre>
</li>
</ol>
</body>
</html> 还没接触smarty,顶贴 其实index.php里面已经有一个$system_folder 定位系统目录了.... 原帖由 xjflyttp 于 2008-8-10 11:32 发表 http://codeigniter.org.cn/forums/images/common/back.gif
其实index.php里面已经有一个$system_folder 定位系统目录了....
两码事
太感谢你了
我就是想他们两能结合在一起,谢谢你给的配置哈!:victory: 按照这个方法在WINDOWS下运行没有问题但是换到linux下就会报错
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /data/demo.17.com/vote/system/application/libraries/Cismarty.php on line 4
怎么回事呢? 我的项目就是CI + smarty 感觉还行。项目快结束了。我还没研究这两个框架。刚接触php不到2月。就是会用 ci没有必要用smarty吧 我的为什么在$this->cismarty->assign();时会有下面的错误
Message: Undefined property: Main:: $cismarty 多谢,问题解决了.
页:
[1]
2