PHP框架中集成Smarty3模板引擎(一) ------ CakePHP 1.3

参考文章:http://blog.csdn.net/kunshan_shenbin/article/details/6212529

cakephp版本:1.3.14

smarty版本:3.1.7

步骤如下:

1. 在app/vendors下新建smarty文件夹,然后把下载下来的smarty包的libs下的文件拷贝进去。

2. 在app目录下新建app_controller.php,代码如下:

class AppController extends Controller {
	
	var $view = 'Smarty';
}
3. 在app/views下新建smarty.php,代码如下:

App::import('Vendor', 'Smarty', array('file' => 'smarty'.DS.'Smarty.class.php'));

class SmartyView extends View {
	
	function __construct (&$controller) {
		
		parent::__construct($controller);
		$this->Smarty = &new Smarty();
		$this->ext= '.tpl';
		$this->Smarty->compile_dir = TMP.'smarty'.DS.'compile'.DS;
		$this->Smarty->cache_dir = TMP.'smarty'.DS.'cache'.DS;
		$this->Smarty->template_dir = VIEWS.DS;
	}

	function _render($___viewFn, $___data_for_view, $___play_safe = true, $loadHelpers = true)
	{
		foreach($___data_for_view as $data => $value)
		{
			if(!is_object($data))
			{
				$this->Smarty->assign($data, $value);
			}
		}
		$this->Smarty->assignByRef('this', $this);
		return $this->Smarty->fetch($___viewFn);
	}
}
4. 在app/views/layouts下新建default.tpl文件,代码如下:

Layout:
<br />
<br />
{$content_for_layout}

接下来我们可以使用smarty来编写测试代码:

在app/controllers下新建tests_controller.php,代码如下:

class TestsController extends AppController {

	//var $view = 'Smarty';
	var $uses = null;
	
	function index() {
		
		$name = 'Shen Bin';
		$date = date("Y-m-d H:i", time());
		
		$this->set(compact('date', 'name'));
	}
}

在app/views/tests下新建index.ctp,代码如下:

{$date}
<hr />
Hello, {$name}!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值