10

CakePhP 2.0 で「ビュー ブロック」を使用する方法を説明できる人はいますか?

Cakephp サイトのドキュメントを読みましたが、初心者ユーザーには多くのことを見逃しています...たとえば、どのファイルが必要なのか、コード内でブロックをどのように呼び出すのか、コードのブロックには独自のフォルダー/が必要ですか?コントローラー/モデル/ビューなど? 私は本当に迷っています!

ブロックをサイドバーとして使用する方法について、誰かが最初から最後まで説明できれば、それは素晴らしいことです。

例としては、別のページで使用したいサイドバーがありますが、ブロック内で呼び出すためにサイドバーを別の要素に分割したい場合があります。

<div class="heading1">
  <h2>Heading 1</h2>
</div>
<div class="ul-list1">
<ul> 
<li>list item 1</li>
<li>list item 2</li>
</ul>
</div>
<div class="heading2">
  <h2>Heading 2</h2>
</div>
<div class="ul-list1">
<ul> 
<li>list item 3</li>
<li>list item 4</li>
</ul>
</div>

これを 2 つの要素 (見出し 1 と見出し 2) に分割します。

ブロックのコードをどのように記述すればよいでしょうか?このコードをどこに挿入し、どのページが必要ですか? (私はこれについて本当に混乱しているので、初心者のCakePhPユーザーに向けてください!)

4

3 に答える 3

7

次のような要素を作成する必要があります。

// app/views/elements/headings.ctp
<?php $this->start('heading1'); ?>
<div class="heading1">
    <h2>Heading 1</h2>
</div>
<div class="ul-list1">
    <ul> 
        <li>list item 1</li>
        <li>list item 2</li>
    </ul>
</div>
<?php $this->end(); ?>


<?php $this->start('heading2'); ?>
<div class="heading2">
    <h2>Heading 2</h2>
</div>

<div class="ul-list1">
    <ul> 
        <li>list item 3</li>
        <li>list item 4</li>
    </ul>
</div>
<?php $this->end(); ?>
// end element file

// include the element first before getting block in the views or layout file.
<?php 
    echo $this->element('headings');
?>

// after that you will able to display block anywhere inside view files or layout also with following statements.

<?php
    // for heading first 
    echo $this->fetch('heading1');

    // for heading second.
    echo $this->fetch('heading2');
?>
于 2013-10-15T05:29:46.757 に答える
1

ビューまたは要素で次のコードを使用できます。

// for start the block code
$this->start('block_name');

// your html code will goes here, even you can also specify the element reference.

$this->end(); // close the block.

レイアウトでは、ビューブロックコードを次のように取得/表示できます

echo $this->fetch('block_name'); // anywhere in the layout.

ビューとレイアウトで同じブロック名を指定したことを確認してください。

于 2013-10-12T01:49:50.280 に答える