zend framework & smarty & layout

网上查了一些资料没发现很好能理解的关于 zend framework、smarty 和 layout 一起使用的方案,自己琢磨了下帖出来分享,大部分代码摘自网络,可能办法比较笨拙,还望高手指正

1、引导文件 index.php

//setup the view renderer
include_once './library/Templater/Templater.php';
$vr = new Zend_Controller_Action_Helper_ViewRenderer();
$vr->setView(new Templater());
$vr->setViewSuffix('tpl');
Zend_Controller_Action_HelperBroker::addHelper($vr);

2、Zend_View_Abstract 的子类 Templater 

     说明:初始化smarty对象,重定义视图对象的方法

  

<?php
class Templater extends Zend_View_Abstract{
        protected $_engine;
 
        public function __construct(){
            require_once('Smarty/Smarty.class.php');  
            $this->_engine = new SmartyAdvancedDisplay();
            //$this->_engine = new Smarty();
            $this->_engine->left_delimiter = '{#';
			$this->_engine->right_delimiter = '#}';
			$this->_engine->compile_dir = './application/views/templates_c';
			$this->_engine->cache_dir = './application/views/templates_c/cache_c';
			$this->_engine->template_dir = './application/views/templates';
            $this->_engine->plugins_dir = './library/Templater/plugins';
        }
 
        public function getEngine()
        {
            return $this->_engine;
        }
 
        public function __set($key, $val)
        {
            $this->_engine->assign($key, $val);
        }
 
        public function __get($key)
        {
            return $this->_engine->get_template_vars($key);
        }
 
        public function __isset($key)
        {
            return $this->_engine->get_template_vars($key) !== null;
        }
 
        public function __unset($key)
        {
            $this->_engine->clear_assign($key);
        }
 
        public function assign($spec, $value = null)
        {
            if (is_array($spec)) {
                $this->_engine->assign($spec);
                return;
            }
 
            $this->_engine->assign($spec, $value);
        }
 
        public function clearVars()
        {
            $this->_engine->clear_all_assign();
        }
 
        public function render($name)
        {
            return $this->_engine->display(strtolower($name));
        }
 
        public function _run()
        { }
    }
?>

  3、Smarty 的子类 SmartyAdvancedDisplay

  说明:重定义display方法,使之能够支持layout

class SmartyAdvancedDisplay extends Smarty{
	public $layout_name = 'default.tpl';
	function display($template,$cache_id,$compile_id,$parent) {
		if (!$this->getTemplateVars('template')) {
			$this->assign('template',$template);
		}
		parent::display('layouts/'.$this->layout_name,$cache_id,$compile_id,$parent);
	}
	
	function setLayoutName($name){
		$this->layout_name = $name;
	}
	
	function setActionTemplateDir($dir){
		$this->assign('template',$dir);
	}
}

  4、controller中动态的加载layout

  

function indexAction() {
		$this->view->title = 'zhudong.me的精彩生活从此开始...(期待ing)';
		$diaryObj = new Diary();
		$tempDiarys = $diaryObj->fetchAll();
		foreach ($tempDiarys as $value) {
			$postdate = gmdate('Y/m/d H:i',$value['postdate']+8*3600);
			$diary['id'] = $value['id'];
			$diary['title'] = $value['title']; 
			$diary['content'] = $value['content']; 
			$diary['postdate'] = $postdate;
			$diarys[] = $diary;
		}
		$this->view->diarys = $diarys;
		//动态加载控制器模版
		//$this->view->getEngine()->setActionTemplateDir('index/index.tpl');
		//动态加载布局模版
		//$this->view->getEngine()->setLayoutName('default.tpl');
	}

  

转载于:https://www.cnblogs.com/zhudong/archive/2011/11/18/2253903.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值