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
哪个变体是正确的?
此外,我似乎有双重渲染:起初我渲染每个片段。然后在渲染最终模板时再次渲染它们。这是正确的方法吗?