【发布时间】:2012-07-09 17:44:33
【问题描述】:
我不明白Zend_Layout 和Zend_View 之间有什么区别。
这是来自Zend_Layout教程的图片。
一切似乎都很容易理解。我们在<head> 中有元素,我们有Header、Navigation 段、Content 段、Sidebar 和Footer。并且很容易理解它们是如何被调用的。但我看不出View 和Layout 之间的区别。为什么Navigation段被称为Layout的属性而Footer和Header被称为View属性?
我测试了Zend_Layout 并交换了它们。我称Navigation 段不是Layout 的属性,而是View 的属性:
echo $this->layout()->nav; // as in tutorial
echo $this->nav; // used to call like this
一切正常。不仅适用于$nav,而且适用于任何变量。那么有什么区别呢?
我在这里附上我的实验代码。 我的实验布局页面由三个主要块组成:
- 标题(标题的html代码),
- 内容(内容块的 html 代码)
- 页脚(html-код 页脚)
这是一个模板脚本:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div header>
<?php echo $this->header ?> // it works
<?php echo $this->layout()->header ?> // and this variant also works
</div>
<div content>
<?php echo $this->content ?> // same thing, it is a View's property
<?php echo $this->layout()->content ?> // And it is a Layout's property
</div>
<div footer>
<?php echo $this->footer ?> // same thing
<?php echo $this->layout->footer() ?> // both work (one or another I mean)
</div>
</body>
</html>
我现在的代码:
$layout = Zend_Layout::startMvc(); // instantiate Layout
$layout->setLayoutPath('\TestPage\views'); // set the path where my layouts live
// And here's the most interesting
// Set Header layout first
$layout->setLayout('header'); // 'header.php' - is my file with html-code of the Header
// I pass only name 'header', and it makes 'header.php' from it.
// Predefined suffix is 'phtml' but I changed it to 'php'
$layout->getView()->button = "Button"; // assign some variable in the Header. Please pay attention, it is View's property
$layout->button_2 = "Button_2"; // and also I can assign this way. It's Layout's property now. And they both work
$headerBlock = $layout->render(); // render my Header and store it in variable
// the same procedures for the Content block
$layout->setLayout('content');
$layout->getView()->one = "One";
$layout->two = "Two";
$contentBlock = $layout->render(); // render and store in the variable
// and the same for the Footer
$layout->setLayout('footer');
$layout->getView()->foot = "Foot";
$layout->foot_2 = "Foot_2";
$footerBlock = $layout->render(); // render and store in the variable
// and finally last stage - render whole layout and echo it
$lout->setLayout('main_template');
$layout->getView()->header = $headerBlock; // again, I can do also $layout->header
$lout->content = $contentBlock;
$lout->getView()->footer = $footerBlock;
echo $lout->render(); // render and echo now.
一切正常,页面显示没有错误。但我不知道我使用Zend_Layout 和Zend_View 是对还是错。像我一样使用Zend_Layout 构建页面是正确的方法吗?有什么区别
echo $this->layout()->header // this
echo $this->header // and this
哪个变种是正确的?
似乎我有双重渲染:首先我渲染每个片段。然后在渲染最终模板时再次渲染它们。这是正确的方法吗?
【问题讨论】:
标签: php model-view-controller zend-framework