2

Zend_Layoutとの違いがわかりませんZend_View

Zend_Layoutこれがチュートリアル の写真です。ここに画像の説明を入力してください

すべてが理解しやすいようです。に要素が<head>ありHeader、、Navigationセグメント、Contentセグメント、SidebarおよびがありFooterます。そして、それらがどのように呼ばれるかを理解するのは簡単です。Viewしかし、との違いはわかりませんLayoutNavigationセグメントがLayoutのプロパティとFooter呼ばHeaderれ、プロパティと呼ばれるのはなぜViewですか?

私はそれらをテストZend_Layoutして交換しました。Navigationセグメントを'sプロパティとしてではなく、Layout'sプロパティとして呼び出しましたView

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

そして、すべてが正常に動作します。だけでなく$nav、任意の変数に対して。では、違いは何ですか?

ここに実験コードを添付します。私の実験レイアウトページは、3つの主要なブロックで構成されています。

  • ヘッダー(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_Layout1つ以上の他のテンプレートを含む特別なタイプのテンプレートです。1つのページにレイアウトを1つだけ配置する必要があります。レイアウトには、ページ間で実際には変更されない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 に答える