2

Zend_Layout我不明白和之间有什么区别Zend_View

这是Zend_Layout教程中的图片。 在此处输入图像描述

一切似乎都很容易理解。我们有元素<head>,我们有HeaderNavigation段,Content段,SidebarFooter。并且很容易理解它们是如何被调用的。View但我看不出和之间的区别Layout。为什么Navigation段被称为Layout'属性和FooterHeader被称为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

哪个变体是正确的?

此外,我似乎有双重渲染:起初我渲染每个片段。然后在渲染最终模板时再次渲染它们。这是正确的方法吗?

4

1 回答 1

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()。

于 2012-07-09T18:13:00.197 回答