频道栏目
首页 > 资讯 > Yii > 正文

Yii框架整合Smarty模板引擎

16-01-07        来源:[db:作者]  
收藏   我要投稿

Smarty作为最成熟使用最多的模板之一,相信大家都比较熟悉。如果还有的同学不是很熟悉,可以参考Smarty教程。不过就算不会也不要紧,我这里尽量详细说明让大家明白。

可以先了解smarty中的assign(),include,display()这几个函数的用法。另外,还需要知道if-else,foreach这两个标签。这就够了!

配置Smarty支持

1、创建一个类,文件名为:CSmarty.php?内容如下:

Yii::getPathOfAlias('application.extensions.smarty') . DIRECTORY_SEPARATOR . 'Smarty.class.php');
define('SMARTY_VIEW_DIR', Yii::getPathOfAlias('application.views'));
class CSmarty extends Smarty {
	const DIR_SEP = DIRECTORY_SEPARATOR;
	function __construct() {
		parent::__construct();
		$this -> template_dir = SMARTY_VIEW_DIR;
		$this -> compile_dir = SMARTY_VIEW_DIR . self::DIR_SEP . 'template_c';
		$this -> caching = true;
		$this -> cache_dir = SMARTY_VIEW_DIR . self::DIR_SEP . 'cache';
		$this -> left_delimiter = '';
		$this -> cache_lifetime = 0;
		// -- 初始全局数据
		$this -> assign('base_url', 'http://www.ttall.net');
		$this -> assign('index_url', 'http://www.ttall.net/index.php');
	}
	function init() {
	}
}

把上述类放到文件目录:?protected/extensions/

2、配置protected/config/main.php
在该文件中加入如下代码:

dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
	'name'=>'易百IT教程网-www.yiibai.com',

	// preloading 'log' component
	'preload'=>array('log'),

	// autoloading model and component classes
	'import'=>array(
		'application.models.*',
		'application.components.*',
		'application.extensions.*',
		'application.extensions.smarty.sysplugins.*',
	),
	// application components
	'components'=>array(
		'user'=>array(
			// enable cookie-based authentication
			'allowAutoLogin'=>true,
		),

		'smarty'=>array(
			'class'=>'application.extensions.CSmarty',
		),

3、找到文件protected/components/Controller.php,加入两个方法:

/**
 * Smarty assign()方法
 *
 */
public function assign($key, $value) {
	Yii::app() -> smarty -> assign($key, $value);
}

/**
 * Smarty display()方法
 *
 */
public function display($view) {
	Yii::app() -> smarty -> display($view);
}

这样,我们就可以在每一个控制器里直接调用这两个Smarty方法了。

相关TAG标签
上一篇:有什么办法可以实现像windows画图一样的效果解决方法
下一篇:用PHP实现的HashTable及说明
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站