• 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 海外寻宝
  • 相关阅读:
    面试后的一些思考
    NEW关键字的三种用法
    关于反射的初步介绍
    REF和OUT关键字的介绍
    关于WPF中TextBox行的选择与显示的问题
    资源字典——程序集之间的资源共享
    rsync 实现断点续传
    CentOS 7 安装 Git
    使用 docker 拉取镜像和创建容器-nginx
    docker安装 之 ---CentOS 7 系统脚本自动安装
  • 原文地址:https://www.cnblogs.com/zhudong/p/2253903.html
Copyright © 2020-2023  润新知