Zend Framework教程之视图组件Zend_View用法详解
本文实例讲述了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://www.zend.com) * @license http://framework.zend.com/license/new-bsd new bsd license * @version $id: view.php 23775 2011-03-01 17:25:24z ralph $ */ /** * abstract master class for extension. */ require_once 'zend/view/abstract.php'; /** * concrete class for handling view scripts. * * @category zend * @package zend_view * @copyright copyright (c) 2005-2011 zend technologies usa inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd new bsd license */ class zend_view extends zend_view_abstract { /** * whether or not to use streams to mimic short tags * @var bool */ private $_useviewstream = false; /** * whether or not to use stream wrapper if short_open_tag is false * @var bool */ private $_usestreamwrapper = false; /** * constructor * * register zend_view_stream stream wrapper if short tags are disabled. * * @param array $config * @return void */ public function __construct($config = array()) { $this->_useviewstream = (bool) ini_get('short_open_tag') ? false : true; if ($this->_useviewstream) { if (!in_array('zend.view', stream_get_wrappers())) { require_once 'zend/view/stream.php'; stream_wrapper_register('zend.view', 'zend_view_stream'); } } if (array_key_exists('usestreamwrapper', $config)) { $this->setusestreamwrapper($config['usestreamwrapper']); } parent::__construct($config); } /** * set flag indicating if stream wrapper should be used if short_open_tag is off * * @param bool $flag * @return zend_view */ public function setusestreamwrapper($flag) { $this->_usestreamwrapper = (bool) $flag; return $this; } /** * should the stream wrapper be used if short_open_tag is off? * * @return bool */ public function usestreamwrapper() { return $this->_usestreamwrapper; } /** * includes the view script in a scope with only public $this variables. * * @param string the view script to execute. */ protected function _run() { if ($this->_useviewstream && $this->usestreamwrapper()) { include 'zend.view://' . func_get_arg(0); } else { include func_get_arg(0); } } }
默认情况会自动通过controller会通过render方法来实例化zend_view, 然后rener到对应的视图文件中。当然可以自己实例化zend_view,然后使用。
action默认指向的文件是和action的名称相同,如果要指定视图文件,可以通过$this->render的相关方法指定.也可以通过addscriptpath和setscriptpath设置视图文件的目录。
例如
$view = new zend_view(); $view->addscriptpath('/www/app/myviews'); $view->addscriptpath('/www/app/viewscomm'); // 如果调用 $view->render('example.php'), zend_view 将 // 首先查找 "/www/app/myviews/example.php", 找不到再找"/www/app/viewscomm/example.php", 如果还找不到,最后查找当前目录下/的"example.php".
zend_view的常用方法
public function __construct($config = array())
构造函数参数
例如
array( 'escape' => array(), 'encoding' => array(), );
常见key:
escape、encoding、basepath、basepathprefix、scriptpath、helperpath、 helperpathprefix、filterpath、filterpathprefix、filter
public function getengine() return the template engine object
public function init()初始化函数
/** * given a base path, sets the script, helper, and filter paths relative to it * * assumes a directory structure of: * <code> * basepath/ * scripts/ * helpers/ * filters/ * </code> * * @param string $path * @param string $prefix prefix to use for helper and filter paths * @return zend_view_abstract */ public function setbasepath($path, $classprefix = 'zend_view') /** * given a base path, add script, helper, and filter paths relative to it * * assumes a directory structure of: * <code> * basepath/ * scripts/ * helpers/ * filters/ * </code> * * @param string $path * @param string $prefix prefix to use for helper and filter paths * @return zend_view_abstract */ public function addbasepath($path, $classprefix = 'zend_view') public function addscriptpath($path)adds to the stack of view script paths in lifo order. public function setscriptpath($path) resets the stack of view script paths. public function getscriptpath($name)return full path to a view script specified by $name public function getscriptpaths()returns an array of all currently set script paths public function addhelperpath($path, $classprefix = 'zend_view_helper_')adds to the stack of helper paths in lifo order. public function sethelperpath($path, $classprefix = 'zend_view_helper_')resets the stack of helper paths. public function gethelperpath($name) get full path to a helper class file specified by $name public function gethelperpaths()returns an array of all currently set helper paths public function gethelper($name) get a helper by name public function gethelpers()get array of all active helpers public function getallpaths() return associative array of path types => paths public function setescape($spec) /** * assigns variables to the view script via differing strategies. * * zend_view::assign('name', $value) assigns a variable called 'name' * with the corresponding $value. * * zend_view::assign($array) assigns the array keys as variable * names (with the corresponding array values). * * @see __set() * @param string|array the assignment strategy to use. * @param mixed (optional) if assigning a named variable, use this * as the value. * @return zend_view_abstract fluent interface * @throws zend_view_exception if $spec is neither a string nor an array, * or if an attempt to set a private or protected member is detected */ public function assign($spec, $value = null)
在controller的action可以通过assign传递参数到视图脚本。
例如
$this->view->assign('roles', $roles); $this->view->assign('num', $num); $this->view->assign('a', $a);
或者也可以用
$this->view->roles=$roles; $this->view->a=$a; public function render($name) processes a view script and returns the output. public function escape($var):escapes a value for output in a view script. public function setencoding($encoding) set encoding to use with htmlentities() and htmlspecialchars() public function getencoding() :return current escape encoding
视图脚本文件中的常见用法:
获取传递过来的值
$this->roles
使用一些常见的助手方法:
$this->baseurl(); $this->url(); $this->paginationcontrol(); $this->partial()
视图常见用法举例
在bootstrap初始化view或者controller的init文件中
/** * initialize the common 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程序设计有所帮助。
推荐阅读
-
Zend Framework教程之视图组件Zend_View用法详解
-
Zend Framework入门教程之Zend_Registry组件用法详解
-
Zend Framework入门教程之Zend_Config组件用法详解
-
Zend Framework入门教程之Zend_View组件用法示例
-
Zend Framework使用Zend_Loader组件动态加载文件和类用法详解
-
Zend Framework教程之Zend_Helpers动作助手ViewRenderer用法详解
-
Zend Framework教程之Application和Bootstrap用法详解,zendbootstrap_PHP教程
-
Zend Framework教程之前端控制器Zend_Controller_Front用法详解,ledcontroller控制器
-
Zend Framework教程之分发器Zend_Controller_Dispatcher用法详解,zendframework_PHP教程
-
Zend Framework教程之视图组件Zend_View用法详解,zendzend_view_PHP教程