这里的
layout() -> content;?>
是比较重要的。表示此处为布局的内容,也就是会动态变化的地方。
这样,运行一下程序
www.localzend.com/layout_demo1/public/
生成的html源码如下
my app headerWelcome to the Zend Framework!
This is your project's main page
Helpful Links:
Zend Framework Website | Zend Framework Manualheader中间部分就是/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"2.在action中使用layout对象
可以通过
$layout = $this->_helper->layout();或者
$helper = $this->_helper->getHelper('Layout');$layout = $helper->getLayoutInstance();获取布局对象。
可以通过如下方式禁用当前action使用布局模式
$layout->disableLayout();可以通过
$layout->setLayout('other');来设置使用另一个布局文件
可以通过来传递赋值
$layout->assign('headertitle', 'app title');$layout->somekey = "value"3.其它获取layout对象的方法
(1)
$layout = Zend_Layout::getMvcInstance();(2)
$layout = $bootstrap->getResource('Layout');四、其它用法,实现原理
具体其它的使用方法可以参考
Zend_Layout_Controller_Action_Helper_Layout类,
Zend_Layout_Controller_Plugin_Layout类
Zend_View_Helper_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 * * Usage: $this->layout()->setLayout('alternate'); * * @return Zend_Layout */ public function layout() { return $this->getLayout(); }}更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
您可能感兴趣的文章:
- zend Framework中的Layout(模块化得布局)详解
- zend framework多模块多布局配置
- Zend Framework连接Mysql数据库实例分析
- Zend Framework+smarty用法实例详解
- Zend Framework教程之Application用法实例详解
- Zend Framework自定义Helper类相关注意事项总结
- Zend Framework教程之Bootstrap类用法概述
- Zend Framework教程之资源(Resources)用法实例详解
- Zend Framework教程之Application和Bootstrap用法详解
- Zend Framework教程之配置文件application.ini解析
- Zend Framework教程之Loader以及PluginLoader用法详解
- Zend Framework教程之Autoloading用法详解
- Zend Framework基本页面布局分析
Top
- 近期文章
- 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
- 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
- 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
- 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
- gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
- 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
- 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
- Laravel从Accel获得5700万美元A轮融资(0个评论)
- 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
- 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
- 近期评论
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..