【问题标题】:Layout and View, what's the difference? As an example of Zend_Layout and Zend_View布局和视图,有什么区别?作为 Zend_Layout 和 Zend_View 的示例
【发布时间】:2012-07-09 17:44:33
【问题描述】:

我不明白Zend_LayoutZend_View 之间有什么区别。

这是来自Zend_Layout教程的图片。

一切似乎都很容易理解。我们在<head> 中有元素,我们有HeaderNavigation 段、Content 段、SidebarFooter。并且很容易理解它们是如何被调用的。但我看不出ViewLayout 之间的区别。为什么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_LayoutZend_View 是对还是错。像我一样使用Zend_Layout 构建页面是正确的方法吗?有什么区别

echo $this->layout()->header // this
echo $this->header  // and this

哪个变种是正确的?

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

【问题讨论】:

    标签: php model-view-controller zend-framework


    【解决方案1】:

    请参阅我发布的其他(类似)问题的答案以了解一些背景信息。 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()。

    【讨论】:

    • 非常感谢您的回答,感谢您的宝贵时间。你的回答真的帮助我理解了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    相关资源
    最近更新 更多