ホームページ  >  記事  >  バックエンド開発  >  Zend Framework チュートリアル、zendzend_view_PHP チュートリアルのビュー コンポーネント Zend_View の使用法の詳細な説明

Zend Framework チュートリアル、zendzend_view_PHP チュートリアルのビュー コンポーネント Zend_View の使用法の詳細な説明

WBOY
WBOYオリジナル
2016-07-12 08:57:40690ブラウズ

Zend Framework チュートリアル、zendzend_view のビュー コンポーネント Zend_View の使用法の詳細な説明

この記事では、Zend Framework チュートリアルのビュー コンポーネント Zend_View の使用方法について説明します。参考のために皆さんと共有してください。詳細は次のとおりです:

Zend_View は、Zend Framework のビュー コンポーネント、MVC のビュー層です。 Zend_View は、ユーザーに直接表示されるアプリケーションのページでもあります。ここではZend_Viewの実装クラスとControllerとの組み合わせ方を紹介します。

実装を見る

Zend_View は主に次のディレクトリ内のクラスを通じて実装されます:

root@coder-671T-M:/library/Zend# ツリー | grep View.php
│ └── 見る/
§── View.php

root@coder-671T-M:/library/Zend/View# ツリー
.
§── Abstract.php
§──Exception.php
§── ヘルパー
│ §── 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
│ §── ナビゲーション
│ │ §── Breadcrumbs.php
│ │ §── HelperAbstract.php
│ │ §── Helper.php
│ │ §── Links.php
│ │ §── Menu.php
│ │ └── サイトマップ.php
│ §── Navigation.php
│ §── PaginationControl.php
│ §── 一部
│ │ └─ Exception.php
│ §── PartialLoop.php
│ §── Partial.php
│ §── プレースホルダー
│ │ §──コンテナ
│ │ │ §── Abstract.php
│ │ │ §── Exception.php
│ │ │ └── Standalone.php
│ │ §── Container.php
│ │ §── レジストリ
│ │ │ └── Exception.php
│ │ └── Registry.php
│ §── Placeholder.php
│ §── RenderToPlaceholder.php
│ §── ServerUrl.php
│ §── TinySrc.php
│ §── Translate.php
│ §── URL.php
│ └── UserAgent.php
§── Interface.php
━── Stream.php

6 ディレクトリ、70 ファイル

Zend_View と Zend_Controller の統合

主に Zend_Controller_Action クラス内で、

リーリー

Zend_View.php クラス

リーリー

デフォルトでは、Zend_View はコントローラーの render メソッドを通じて自動的にインスタンス化され、対応するビュー ファイルに再レンダリングされます。もちろん、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

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

更多关于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用法详解

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1106904.htmlTechArticleZend Framework教程之视图组件Zend_View用法详解,zendzend_view 本文实例讲述了Zend Framework教程之视图组件Zend_View用法。分享给大家供大家参考,具...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。