当前位置:主页 > 样式模版 >正文

公子欢喜新欢-Zend Framework教程之Zend_Layout布局助手详解

2021-09-19 583 样式模版

本文实例讲述了Zend Framework教程之Zend_Layout公子欢喜新欢助手。分享给大家供大家参考,具体如下:

公子欢喜新欢的作用和公子欢喜新欢的作用类似。可以认为是把网站通用、公共的部分拿出来作为通用的页面框架。例如一个基本的web页面,可能页面的头和尾都是一样,不一样的可能只是内容body部分不一样,可以把公共的部分做成公子欢喜新欢。不仅可以提高开发效率,也为后期的维护带来方便。

首先用zend studio创建一个基本的zend framework项目:layout_demo1

应用配置文件/layout_demo2/application/configs/application.ini,加入如下配置

resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" resources.frontController.params.displayExceptions = 0 resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/" [st**ing : production]

2.相应的目录和公子欢喜新欢公子欢喜新欢文件 /layout_demo2/application/layouts/scripts/layout.phtml

my app header layout() -> content;?> header

layout() -> content;?>

是比较重要的。表示此处为公子欢喜新欢的内容,也就是会动态变化的地方。

www.localzend.com/layout_demo1/public/

my app header a:link, a:visited { color: #0398CA; } span#zf-name { color: #91BE3F; } div#welcome { color: #FFFFFF; background-im**e: url(http://framework.zend.com/im**es/bkg_header.jpg); width: 600px; height: 400px; border: 2px solid #444444; overflow: hidden; text-align: center; } div#more-information { background-im**e: url(http://framework.zend.com/im**es/bkg_body-bottom.gif); height: 100%; } Welcome to the Zend Framework! This is your project's main p**e

Helpful Links: Zend Framework Website | Zend Framework Manual

header

中间部分就是/layout_demo1/application/views/scripts/index/index.phtml的内容。

注入:可以通过zf的命令工具自动生成layout的配置和文件。

zf enable layout

1.自定义存放位置和名称可以通过application.ini配置文件配置公子欢喜新欢文件的存放位置以及公子欢喜新欢文件的名称,例如:

resources.layout.layoutPath = APPLICATION_PATH "/mylayouts/scripts" resources.layout.layout = "mylayout"

$layout = $this->_helper->layout();

$helper = $this->_helper->getHelper('Layout'); $layout = $helper->getLayoutInstance();

$layout->disableLayout();

$layout->setLayout('other');

$layout->assign('headertitle', 'app title'); $layout->somekey = "value"

$layout = Zend_Layout::getMvcInstance();

$layout = $bootstrap->getResource('Layout');

Zend_Layout_Controller_Action_Helper_Layout类,

Zend_Layout_Controller_Plugin_Layout类

setLayoutInstance($layout); } else { /** * @see Zend_Layout */ require_once 'Zend/Layout.php'; $layout = Zend_Layout::getMvcInstance(); } if (null !== $layout) { $pluginClass = $layout->getPluginClass(); $front = $this->getFrontController(); if ($front->hasPlugin($pluginClass)) { $plugin = $front->getPlugin($pluginClass); $plugin->setLayoutActionHelper($this); } } } public function init() { $this->_isActionControllerSuccessful = false; } /** * Get front controller instance * * @return Zend_Controller_Front */ public function getFrontController() { if (null === $this->_frontController) { /** * @see Zend_Controller_Front */ require_once 'Zend/Controller/Front.php'; $this->_frontController = Zend_Controller_Front::getInstance(); } return $this->_frontController; } /** * Get layout object * * @return Zend_Layout */ public function getLayoutInstance() { if (null === $this->_layout) { /** * @see Zend_Layout */ require_once 'Zend/Layout.php'; if (null === ($this->_layout = Zend_Layout::getMvcInstance())) { $this->_layout = new Zend_Layout(); } } return $this->_layout; } /** * Set layout object * * @param Zend_Layout $layout * @return Zend_Layout_Controller_Action_Helper_Layout */ public function setLayoutInstance(Zend_Layout $layout) { $this->_layout = $layout; return $this; } /** * Mark Action Controller (according to this plugin) as Running successfully * * @return Zend_Layout_Controller_Action_Helper_Layout */ public function postDispatch() { $this->_isActionControllerSuccessful = true; return $this; } /** * Did the previous action successfully complete? * * @return bool */ public function isActionControllerSuccessful() { return $this->_isActionControllerSuccessful; } /** * Strategy pattern; call object as method * * Returns layout object * * @return Zend_Layout */ public function direct() { return $this->getLayoutInstance(); } /** * Proxy method calls to layout object * * @param string $method * @param array $args * @return mixed */ public function __call($method, $args) { $layout = $this->getLayoutInstance(); if (method_exists($layout, $method)) { return call_user_func_array(array($layout, $method), $args); } require_once 'Zend/Layout/Exception.php'; throw new Zend_Layout_Exception(sprintf("Invalid method '%s' called on layout action helper", $method)); } }

setLayout($layout); } } /** * Retrieve layout object * * @return Zend_Layout */ public function getLayout() { return $this->_layout; } /** * Set layout object * * @param Zend_Layout $layout * @return Zend_Layout_Controller_Plugin_Layout */ public function setLayout(Zend_Layout $layout) { $this->_layout = $layout; return $this; } /** * Set layout action helper * * @param Zend_Layout_Controller_Action_Helper_Layout $layoutActionHelper * @return Zend_Layout_Controller_Plugin_Layout */ public function setLayoutActionHelper(Zend_Layout_Controller_Action_Helper_Layout $layoutActionHelper) { $this->_layoutActionHelper = $layoutActionHelper; return $this; } /** * Retrieve layout action helper * * @return Zend_Layout_Controller_Action_Helper_Layout */ public function getLayoutActionHelper() { return $this->_layoutActionHelper; } /** * postDispatch() plugin hook -- render layout * * @param Zend_Controller_Request_Abstract $request * @return void */ public function postDispatch(Zend_Controller_Request_Abstract $request) { $layout = $this->getLayout(); $helper = $this->getLayoutActionHelper(); // Return early if forward detected if (!$request->isDispatched() || $this->getResponse()->isRedirect() || ($layout->getMvcSuccessfulActionOnly() && (!empty($helper) && !$helper->isActionControllerSuccessful()))) { return; } // Return early if layout has been disabled if (!$layout->isEnabled()) { return; } $response = $this->getResponse(); $content = $response->getBody(true); $contentKey = $layout->getContentKey(); if (isset($content['default'])) { $content[$contentKey] = $content['default']; } if ('default' != $contentKey) { unset($content['default']); } $layout->assign($content); $fullContent = null; $obStartLevel = ob_get_level(); try { $fullContent = $layout->render(); $response->setBody($fullContent); } catch (Exception $e) { while (ob_get_level() > $obStartLevel) { $fullContent .= ob_get_clean(); } $request->setParam('layoutFullContent', $fullContent); $request->setParam('layoutContent', $layout->content); $response->setBody(null); throw $e; } } }

_layout) { require_once 'Zend/Layout.php'; $this->_layout = Zend_Layout::getMvcInstance(); if (null === $this->_layout) { // Implicitly creates layout object $this->_layout = new Zend_Layout(); } } return $this->_layout; } /** * Set layout object * * @param Zend_Layout $layout * @return Zend_Layout_Controller_Action_Helper_Layout */ public function setLayout(Zend_Layout $layout) { $this->_layout = $layout; return $this; } /** * Return layout object * * Us**e: $this->layout()->setLayout('alternate'); * * @return Zend_Layout */ public function layout() { return $this->getLayout(); } }



版权保护: 本文由 定结网站建设资源网 原创,转载请保留链接: http://www.21windows.com/RrboX9Pu6n/133.html

网站主人定结网站建设资源网
本文实例讲述了Zend Framework教程之Zend_Layout公子欢喜新欢助手。分享给大家供大家参考,具体如下:公子欢喜新欢的作用和公子欢喜新欢的作用类似。可以认为是把网站通用、公共的部分拿出来作为通用的页面框架。例如一个基本的web页面,可能页面的头和尾都是一样,不一样的可能只是内容body部分不一样,可以把公共的部分做成公子欢喜新欢。不仅可以提高开发效率,也为后期的维护带来方便。首先用zend studio创建一个基本的zend framework项目:layo
  • 30849文章总数
  • 23094访问次数
  • 建站天数
  • 推荐文章

    热门文章

    标签

    友情链接