Zend_Layout
との違いがわかりませんZend_View
。
Zend_Layout
これがチュートリアル
の写真です。
すべてが理解しやすいようです。に要素が<head>
ありHeader
、、Navigation
セグメント、Content
セグメント、Sidebar
およびがありFooter
ます。そして、それらがどのように呼ばれるかを理解するのは簡単です。View
しかし、との違いはわかりませんLayout
。Navigation
セグメントが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
どのバリアントが正しいですか?
また、私は二重レンダリングを行っているようです。最初は各セグメントをレンダリングします。そして、最終的なテンプレートをレンダリングするときに、それらを再度レンダリングします。これは正しい方法ですか?