亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 開發 > PHP > 正文

Zend Framework教程之視圖組件Zend_View用法詳解

2024-05-04 23:43:35
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了Zend Framework教程之視圖組件Zend_View用法,較為詳細的分析了試圖組件Zend_View原理并結合實例形式分析了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 $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

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


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产精品成人v| 欧美亚洲另类在线| 日韩av电影手机在线观看| 亚洲国产精品女人久久久| 日韩av在线精品| 最近更新的2019中文字幕| 久久久女女女女999久久| 伊人精品在线观看| 亚洲天堂免费观看| 欧美激情亚洲精品| 欧美激情手机在线视频| 精品国产乱码久久久久久天美| 欧美激情视频在线观看| 欧美精品午夜视频| 欧美成人四级hd版| 亚洲精品美女网站| 欧美区在线播放| 亚洲欧美日韩在线一区| 成人字幕网zmw| 中文字幕日韩精品有码视频| 亚洲精品动漫100p| 日韩av影院在线观看| 中日韩午夜理伦电影免费| 日韩精品中文字幕在线播放| 亚洲一区999| 欧美精品xxx| 久久久999国产精品| 高清欧美电影在线| 久久久久久一区二区三区| 成人免费高清完整版在线观看| 国产69精品99久久久久久宅男| 亚洲成人激情视频| 日韩成人在线免费观看| 日韩成人在线电影网| 亚洲一级片在线看| 亚洲第一视频在线观看| 黄色一区二区在线观看| 国产精品一区二区久久久| 日韩精品免费在线| 久久夜色精品亚洲噜噜国产mv| 欧美专区第一页| 欧美激情综合亚洲一二区| 亚洲一区二区少妇| 92版电视剧仙鹤神针在线观看| 欧美一区二区色| 亚州av一区二区| 日韩中文字幕精品| 欧美视频专区一二在线观看| 久久久久久久999精品视频| 精品国偷自产在线视频| 国产综合在线看| 91精品国产电影| 91亚洲国产成人久久精品网站| 亚洲人成自拍网站| 91免费高清视频| 午夜精品久久久久久久99热浪潮| 国产在线日韩在线| 日韩欧美国产中文字幕| 国产精品xxx视频| 奇米四色中文综合久久| 欧美激情影音先锋| 久久99国产精品自在自在app| 亚洲国模精品私拍| 国产999在线观看| 亚洲精选中文字幕| 欧美性猛交xxxx黑人| 91精品国产乱码久久久久久蜜臀| 久久国产精品久久久久久| 国产成+人+综合+亚洲欧美丁香花| 亚洲欧美日韩一区二区在线| 亚洲成人精品久久| 国产成人a亚洲精品| 91热福利电影| 国内精品久久久久久| 亚洲人午夜精品免费| 亚洲女同精品视频| 亚洲精品日韩激情在线电影| 日韩精品免费视频| 午夜精品蜜臀一区二区三区免费| 亚洲欧美精品在线| 国产综合久久久久| 在线视频免费一区二区| 日韩欧美a级成人黄色| 在线观看国产成人av片| 91久久久久久久一区二区| 国产精品美女网站| 91精品国产综合久久久久久久久| 久久99久久99精品免观看粉嫩| 国产精品91视频| 国产精品99蜜臀久久不卡二区| 国产成人精品午夜| 日本午夜精品理论片a级appf发布| 8090成年在线看片午夜| 国产剧情久久久久久| 91av在线播放视频| 亚洲欧美制服丝袜| 国产美女精品免费电影| 久久在线精品视频| 国产精品欧美在线| 日韩av在线播放资源| 日本老师69xxx| 一区二区三区在线播放欧美| 欧美极度另类性三渗透| 91chinesevideo永久地址| 亚洲天堂免费在线| 欧美极度另类性三渗透| 欧美一级片久久久久久久| 992tv成人免费视频| 95av在线视频| 欧美成人剧情片在线观看| 性欧美xxxx视频在线观看| 成人444kkkk在线观看| 成人xxxx视频| 国产性猛交xxxx免费看久久| 九九热精品视频| 亚洲裸体xxxx| 中文字幕精品久久久久| 国产国产精品人在线视| 国产精品视频永久免费播放| 国产成人97精品免费看片| 欧美第一淫aaasss性| 欧美性xxxxx极品| 亚洲欧美国产精品| 欧美激情中文字幕乱码免费| 国产精品美女主播在线观看纯欲| 久久久久久久久久av| 中文字幕欧美日韩va免费视频| 国产精品一区二区女厕厕| 亚洲毛片在线免费观看| 欧美亚洲成人免费| 亚洲国产精品热久久| 国产精品欧美久久久| 岛国av午夜精品| 狠狠色噜噜狠狠狠狠97| 欧美最顶级的aⅴ艳星| 日日狠狠久久偷偷四色综合免费| 亚洲精品网址在线观看| 九九视频这里只有精品| 5252色成人免费视频| 国产精品va在线播放| 亚洲性线免费观看视频成熟| 欧美一级免费视频| 亚洲色图综合久久| 久久久免费电影| 成人午夜激情免费视频| 91欧美精品成人综合在线观看| 国产亚洲精品一区二555| 欧美综合激情网| 成人欧美一区二区三区黑人孕妇| 日韩欧美国产视频| 日韩在线中文视频| 久久精品视频在线播放| 18一19gay欧美视频网站| 亚洲欧美综合v| 麻豆一区二区在线观看| 国产欧美一区二区白浆黑人| 国产精自产拍久久久久久蜜| 91精品中文在线| 国产精品户外野外| 97国产精品人人爽人人做| 久久久亚洲影院你懂的| 97在线观看免费| 日韩在线不卡视频| 亚洲一区二区中文|