什么是布局,什么是 ZF 中的视图?我应该何时使用以及使用谁的变量,为什么? [英] What is Layout and what is View in ZF? When and whose variables should I use and why?

查看:32
本文介绍了什么是布局,什么是 ZF 中的视图?我应该何时使用以及使用谁的变量,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解何时使用 Layout 的变量以及何时使用 View 的变量来获取页面上的页面段.这是他们的 Layout 包教程的图片($this 表示到处都是 View 实例):

I can't understand when to use Layout's variables and when to use View's variables to get page segments on the page. Here is the picture form their Layout package tutorial ($this means the View instance everywhere):

为什么将 NavigationContentSidebar 段作为 Layout 变量获得?

Why Navigation, Content and Sidebar segments are got as Layout variables?

$this->layout()->nav;

但是HeadTitleHeadScriptHeadStylesheet 是直接从View 获取的?

But HeadTitle, HeadScript, HeadStylesheet are got straightly from View?

$this->headTitle(); // I know that this is a placeholder view helper. 
                    // But this segment of the page logically belongs to Layout.
                    // and it has to be called smth like view->layout->placeholder 

以及为什么 HeaderFooter 来自 View 的某些 partial 方法而不是 Layout 的属性?

And why Header and Footer are from some partial method of the View but not Layout's properties?

$this->partial('header.phtml');

我已尝试更改它们,两种方式都可以正常工作:

I've tried to change them and both ways work fine:

echo $this->nav; // I assigned navigation segment script to the View and it works;

我尝试将 Footer 段脚本分配给 Layout 并且它也有效:

I tried to assign Footer segment script to the Layout and it also works:

$layout->footer = $footer;
echo $this->layout()->footer;  // it also works, it's displayed on the page

任何方式都可以应用于页面上的任何变量.例如,在 Navigation 段中,我有很多变量要显示,我可以使用两种方式输出它们 - 一个变量作为 Layout 的属性,另一个变量作为 查看的属性.

Any of the ways may be applied to any variable on the page. For example in Navigation segment I have a lot of variables to display and I can output them using both ways - one variable as Layout's property, another one sa View's property.

那么正确使用它们的规则是什么?我什么时候应该使用 View 的变量,什么时候使用 Layout 的变量?

So what is the rule to use them right way? When should I use View's variables and when Layout's ones?

推荐答案

我同意这从文档中不是很清楚,而且我不认为 $this->layout()->nav 完全解释了.可能有帮助的几点:

I agree that this isn't very clear from the documentation, and I don't think $this->layout()->nav is explained at all. A few points that might help:

  • $this->layout() 实际上是对布局视图助手的调用,它返回 Zend_Layout 的当前实例.
  • Zend_Layout 注册自己的占位符助手(使用键 'Zend_Layout'),并默认在其中创建一个 'content' 变量.
  • Zend_Layout 类有一个神奇的 __get() 方法,它代理任何成员变量调用到其注册的占位符容器.所以调用 $this->layout()->content 是另一种写法 $this->placeholder('Zend_Layout')->content
  • Zend_Layout 类还有一个神奇的 __set() 方法,它将存储的数据代理到占位符类.所以 $layout->footer = 'foo' 和在视图中调用 $this->placeholder('Zend_Layout')->footer = 'foo' 是一样的代码>
  • $this->layout() is actually a call to the layout view helper, which returns the current instance of Zend_Layout.
  • Zend_Layout registers its own placeholder helper (with the key 'Zend_Layout'), and by default creates a 'content' variable in this.
  • the Zend_Layout class has a magic __get() method which proxies any member variable calls over to its registered placeholder container. So calling $this->layout()->content is another way of writing $this->placeholder('Zend_Layout')->content
  • the Zend_Layout class also has a magic __set() method that proxies stored data to the placeholder class. So $layout->footer = 'foo' is the same as calling $this->placeholder('Zend_Layout')->footer = 'foo' in the view

考虑到这一点:

为什么将导航、内容和侧边栏段作为布局变量?

Why Navigation, Content and Sidebar segments are got as Layout variables?

因为这些正在访问存储在 Zend_Layout 占位符中的数据.你也可以使用 $this->placeholder('Zend_Layout')->content

As these are accessing data stored in Zend_Layout's placeholder. You could also use $this->placeholder('Zend_Layout')->content

但是 HeadTitle、HeadScript、HeadStylesheet 是直接从 View 获取的吗?

But HeadTitle, HeadScript, HeadStylesheet are got straightly from View?

这些是视图助手.

为什么 Header 和 Footer 来自 View 的某些部分方法而不是 Layout 的属性?

And why Header and Footer are from some partial method of the View but not Layout's properties?

这是从其他模板访问内容的标准方式.

This is the standard way of accessing content from other templates.

一般来说,假设使用视图对象是访问数据的正确方式.仅当您知道数据位于布局占位符中时才使用布局对象/助手.

In general, assume that using the view object is the correct way to access the data. Use the layout object/helper only if you know the data is in the layout placeholder.

使用占位符相对于部分的优势在于您可以在几个不同的地方访问和修改它们,包括视图本身.例如,假设您有一个侧边栏存储在部分中.如果您将其存储在 Zend_Layout 占位符中(例如在控制器插件中),则您可以为控制器中的某些操作覆盖它:

The advantage of using placeholders over partials is that you can access and modify them in several different places, including in the view itself. For example say you had a sidebar which is stored in a partial. If you were to store this in the Zend_Layout placeholder instead (for example in a controller plugin), you can then override this for certain actions in the controller:

public function someAction()
{
    $this->view->layout()->sidebar = 'Some other sidebar content';
}

或在视图脚本中:

<?php $this->layout()->sidebar = 'Content for this page only'; ?>

这篇关于什么是布局,什么是 ZF 中的视图?我应该何时使用以及使用谁的变量,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆