我无法理解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
,也适用于任何变量。那有什么区别?
我在这里附上我的实验代码。 我的实验布局页面包含三个主要块:
这是一个模板脚本:
<!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
哪种变体是正确的?
此外,我似乎有双重渲染:首先我渲染每个段。然后,当我渲染我的最终模板时,我再次渲染它们。这是正确的方法吗?
答案 0 :(得分:3)
在某些背景下,查看我发布到您的其他(类似)问题的答案。 Zend_View
用于呈现模板。 Zend_Layout
是一种特殊类型的模板,包含一个或多个其他模板。 您应该只在页面上有一个布局。我们的想法是,布局包含HTML的部分,这些部分在页面之间并没有真正改变。
您的代码可以简化为:
主要布局文件:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php echo $this->render('_header.phtml')?>
<div content>
<?php echo $this->layout()->content ?>
</div>
<?php echo $this->render('_footer.phtml)?>
</body>
</html>
用法:
$layout = new Zend_Layout();
$layout->setLayoutPath('\TestPage\views');
$layout->setLayout('layout'); // this should be whatever you named your layout file
$view = new Zend_View();
$layout->content = $view->render('main_template.php'); // this is the part that changes between pages
echo $layout->render();
如果您没有使用Zend Framework控制器类(您似乎不是这样),请不要调用startMvc()。