• 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');
    	}
    

      

    http://www.hwxb.net 海外寻宝
  • 相关阅读:
    拓扑检查(ArcEngine)
    IMap.SelectByShape 方法选择要素
    根据图层获取要素的渲染颜色
    ArcEngine(AE)符号选择器及符号恢复功能【转载】
    版面元素介绍IElement
    示例:由线段生成一个环,由环构成一个多边形(可以包含多个环的多边形集)
    AO的Display对象简介二【转载】
    显示要素层中的某些要素
    示例:从GraphicsContain中取出一个元素
    示例:从Table中获取SelectionSet
  • 原文地址:https://www.cnblogs.com/zhudong/p/2253903.html
Copyright © 2020-2023  润新知