您好,欢迎访问一九零五行业门户网

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

zend framework教程之视图组件zend_view用法详解,zendzend_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 * /.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类
_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:* * basepath/* scripts/* helpers/* filters/* ** @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:* * basepath/* scripts/* helpers/* filters/* ** @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 $namepublic function getscriptpaths()returns an array of all currently set script pathspublic 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 $namepublic function gethelperpaths()returns an array of all currently set helper pathspublic function gethelper($name) get a helper by namepublic function gethelpers()get array of all active helperspublic function getallpaths() return associative array of path types => pathspublic 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
data as $item) : ?> item1);?>

更多关于zend相关内容感兴趣的读者可查看本站专题:《zend framework框架入门教程》、《php优秀开发框架总结》、《yii框架入门及常用技巧总结》、《thinkphp入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
您可能感兴趣的文章:zend framework教程之autoloading用法详解zend framework教程之resource autoloading用法实例zend framework教程之mvc框架的controller用法分析zend framework教程之路由功能zend_controller_router详解zend framework教程之zend_controller_plugin插件用法详解zend framework教程之响应对象的封装zend_controller_response实例详解zend framework教程之请求对象的封装zend_controller_request实例详解zend framework教程之动作的基类zend_controller_action详解zend framework教程之分发器zend_controller_dispatcher用法详解zend framework教程之前端控制器zend_controller_front用法详解zend framework教程之loader以及pluginloader用法详解
http://www.bkjia.com/phpjc/1106904.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1106904.htmltecharticlezend framework教程之视图组件zend_view用法详解,zendzend_view 本文实例讲述了zend framework教程之视图组件zend_view用法。分享给大家供大家参考,具...
其它类似信息

推荐信息