Zend Framework教程之视图组件Zend_View用法详解

时间:2021-05-25

本文实例讲述了Zend Framework教程之视图组件Zend_View用法。分享给大家供大家参考,具体如下:

Zend_View是Zend Framework的视图组件,MVC中的视图层。 Zend_View也是应用的直接对用户展示的页面。这里介绍一下Zend_View的实现类,以及如何和Controller结合在一起的。

View的实现

Zend_View的实现主要是通过如下目录的类实现:

root@coder-671T-M:/library/Zend# tree | grep View.php
│ └── View/
├── View.php

root@coder-671T-M:/library/Zend/View# tree
.
├── Abstract.php
├── Exception.php
├── Helper
│ ├── Abstract.php
│ ├── Action.php
│ ├── BaseUrl.php
│ ├── Currency.php
│ ├── Cycle.php
│ ├── DeclareVars.php
│ ├── Doctype.php
│ ├── Fieldset.php
│ ├── FormButton.php
│ ├── FormCheckbox.php
│ ├── FormElement.php
│ ├── FormErrors.php
│ ├── FormFile.php
│ ├── FormHidden.php
│ ├── FormImage.php
│ ├── FormLabel.php
│ ├── FormMultiCheckbox.php
│ ├── FormNote.php
│ ├── FormPassword.php
│ ├── Form.php
│ ├── FormRadio.php
│ ├── FormReset.php
│ ├── FormSelect.php
│ ├── FormSubmit.php
│ ├── FormTextarea.php
│ ├── FormText.php
│ ├── Gravatar.php
│ ├── HeadLink.php
│ ├── HeadMeta.php
│ ├── HeadScript.php
│ ├── HeadStyle.php
│ ├── HeadTitle.php
│ ├── HtmlElement.php
│ ├── HtmlFlash.php
│ ├── HtmlList.php
│ ├── HtmlObject.php
│ ├── HtmlPage.php
│ ├── HtmlQuicktime.php
│ ├── InlineScript.php
│ ├── Interface.php
│ ├── Json.php
│ ├── Layout.php
│ ├── Navigation
│ │ ├── Breadcrumbs.php
│ │ ├── HelperAbstract.php
│ │ ├── Helper.php
│ │ ├── Links.php
│ │ ├── Menu.php
│ │ └── Sitemap.php
│ ├── Navigation.php
│ ├── PaginationControl.php
│ ├── Partial
│ │ └── Exception.php
│ ├── PartialLoop.php
│ ├── Partial.php
│ ├── Placeholder
│ │ ├── Container
│ │ │ ├── Abstract.php
│ │ │ ├── Exception.php
│ │ │ └── Standalone.php
│ │ ├── Container.php
│ │ ├── Registry
│ │ │ └── Exception.php
│ │ └── Registry.php
│ ├── Placeholder.php
│ ├── RenderToPlaceholder.php
│ ├── ServerUrl.php
│ ├── TinySrc.php
│ ├── Translate.php
│ ├── Url.php
│ └── UserAgent.php
├── Interface.php
└── Stream.php

6 directories, 70 files

Zend_View和Zend_Controller的整合

主要在Zend_Controller_Action类中,

/** * Initialize View object * * Initializes {@link $view} if not otherwise a Zend_View_Interface. * * If {@link $view} is not otherwise set, instantiates a new Zend_View * object, using the 'views' subdirectory at the same level as the * controller directory for the current module as the base directory. * It uses this to set the following: * - script path = views/scripts/ * - helper path = views/helpers/ * - filter path = views/filters/ * * @return Zend_View_Interface * @throws Zend_Controller_Exception if base view directory does not exist */ public function initView() { if (!$this->getInvokeArg('noViewRenderer') && $this->_helper->hasHelper('viewRenderer')) { return $this->view; } require_once 'Zend/View/Interface.php'; if (isset($this->view) && ($this->view instanceof Zend_View_Interface)) { return $this->view; } $request = $this->getRequest(); $module = $request->getModuleName(); $dirs = $this->getFrontController()->getControllerDirectory(); if (empty($module) || !isset($dirs[$module])) { $module = $this->getFrontController()->getDispatcher()->getDefaultModule(); } $baseDir = dirname($dirs[$module]) . DIRECTORY_SEPARATOR . 'views'; if (!file_exists($baseDir) || !is_dir($baseDir)) { require_once 'Zend/Controller/Exception.php'; throw new Zend_Controller_Exception('Missing base view directory ("' . $baseDir . '")'); } require_once 'Zend/View.php'; $this->view = new Zend_View(array('basePath' => $baseDir)); return $this->view; } /** * Render a view * * Renders a view. By default, views are found in the view script path as * <controller>/<action>.phtml. You may change the script suffix by * resetting {@link $viewSuffix}. You may omit the controller directory * prefix by specifying boolean true for $noController. * * By default, the rendered contents are appended to the response. You may * specify the named body content segment to set by specifying a $name. * * @see Zend_Controller_Response_Abstract::appendBody() * @param string|null $action Defaults to action registered in request object * @param string|null $name Response object named path segment to use; defaults to null * @param bool $noController Defaults to false; i.e. use controller name as subdir in which to search for view script * @return void */ public function render($action = null, $name = null, $noController = false) { if (!$this->getInvokeArg('noViewRenderer') && $this->_helper->hasHelper('viewRenderer')) { return $this->_helper->viewRenderer->render($action, $name, $noController); } $view = $this->initView(); $script = $this->getViewScript($action, $noController); $this->getResponse()->appendBody( $view->render($script), $name ); } /** * Render a given view script * * Similar to {@link render()}, this method renders a view script. Unlike render(), * however, it does not autodetermine the view script via {@link getViewScript()}, * but instead renders the script passed to it. Use this if you know the * exact view script name and path you wish to use, or if using paths that do not * conform to the spec defined with getViewScript(). * * By default, the rendered contents are appended to the response. You may * specify the named body content segment to set by specifying a $name. * * @param string $script * @param string $name * @return void */ public function renderScript($script, $name = null) { if (!$this->getInvokeArg('noViewRenderer') && $this->_helper->hasHelper('viewRenderer')) { return $this->_helper->viewRenderer->renderScript($script, $name); } $view = $this->initView(); $this->getResponse()->appendBody( $view->render($script), $name ); }

Zend_View.php类

<?php/** * Zend Framework * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@zend.com so we can send you a copy immediately. * * @category Zend * @package Zend_View * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://mon view helper */protected function _initViewHelper(){ $boot=$this->bootstrap('View'); $view = $boot->getResource('View'); $view->setHelperPath('Sql/View/Helper', 'Sql_View_Helper');}

action中

/** * * @return void */public function listAction(){ $this->view->assign('data', $data);}

视图文件

list.phtml

<?php foreach ($this->data as $item) : ?><tr style="height: 19px;"> <td class="datagrid-cell"><?php echo($item->item1);?></td></tr><?php endforeach; ?>

更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章