php - 布局和 View 有什么区别?以 Zend_Layout 和 Zend_View 为例

标签 php model-view-controller zend-framework

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

这是来自Zend_Layout的图片教程。 enter image description here

一切看起来都很容易理解。我们有 <head> 中的元素,我们有Header , Navigation段,Content段,SidebarFooter 。而且很容易理解它们的名称。但我看不出 View 之间的区别和Layout 。为什么Navigation段称为 Layout的属性(property)和 FooterHeader称为 View属性(property)?

我测试了Zend_Layout并交换了它们。我调用了Navigation段不作为 Layout的属性(property),但作为 View的属性:

echo $this->layout()->nav;  // as in tutorial
echo $this->nav;    // used to call like this

一切正常。不仅仅是$nav ,但对于任意变量。那么有什么区别呢?

我在这里附上我的实验代码。 我的实验布局页面由三个主要 block 组成:

  • 标题(标题的 html 代码),
  • 内容(内容 block 的 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

哪一种变体是正确的?

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

最佳答案

请参阅我针对您的其他(类似)问题发布的答案,了解一些背景信息。 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 Controller 类(您似乎没有使用),请不要调用 startMvc()。

关于php - 布局和 View 有什么区别?以 Zend_Layout 和 Zend_View 为例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11400291/

相关文章:

javascript - 有没有办法同步服务器端和客户端的验证过程?

ruby-on-rails - Rails 3中的分层MVC?

java - MVC 或 Rest 或两者

php - 获取当前 Controller

php - 两个 MySQL 服务器之间的行为差​​异(错误 1292)

php - mysql逐行插入数据

php - 如何关闭使用 OOP 方法打开的 SQLite 数据库?

php - 如何通过php在整个网络中搜索特定的URL

ruby-on-rails - 如何在 Rails 中的模型和 Controller 之间共享代码?

php - 在 "put"方法中获取 zend 框架中的 post 参数