CakePHP ビューの規約

 ・ビュー名

  PeopleContorollerのgetReady()メソッドは、ビューテンプレートとして

  app/views/people/get_ready.ctp を探す。


・ヘルパー

  ヘルパーは、複数のビューで共通の機能をまとめたクラスのこと。

  JsHelperは、Ajaxリクエストを簡単に扱える。


・AppView

  src/View/AppView.php

  initialize()で、全てのビューで使用されるヘルパーを読み込む。

  $this->loadHelper('MyUtils');


・ビューテンプレート

 ・echo

   <?php echo $var; ?>

   <?= $var ?>

 ・foreach

   <ul>

   <?php foreach ($todo as %itme): ?> // :コロンに注意

     <li><?= $item ?></li>

   <?php endforeach; ?>

   </ul>


 ・if/elseif/else

   <?php if ($username === 'sally'): ?>

    <h3>hello, Sally</h3>

   <?php elseif ($username === 'joe'): ?>

    <h3>hello, Joe</h3>

   <?php else: ?>

    <h3>hello, someone</h3>

   <?php endif; ?>


・ビュー変数

  コントローラで set() で設定した変数は、ビューやレイアウトで使用可能。

  $this->set('activeMenuButton', 'posts');


  h()関数でエスケープできる。

  <?= h($user->bio); ?>


・ビューの継承

  親側で fetch() で定義された変数に、子側で assign() で値を設定する。

  fetch('content') には、assign()されなかった子側の全ての内容が入る。

  <!-- 親 -->

  <h1><?= $this->fetch('title') ?><h1>

  <?= $this->fetch('content') ?>

  <div class="actions">

   <h3>action</h3>

   <ul>

   <?> $this->fetch('sidebar') ?>

   </ul>

  </div>


  <!-- 子 -->

  <?php

   $this->extend('/Common/view');

   $this->assign('title', $post);

   $this->start('sidebar');

  ?>

  <li>

  <?php echo $this->Html-link('edit', ['action' => 'edit', $post->id]); ?>

  </li>

  <?php $this->end(); ?>

  <?= h($post->body) ?> // 'content'へ入る


・ビューブロック

 ・ブロックの作成

  $this->start('sidebar');

  echo $this->element('sidebar/recent_topics');

  echo $this->element('sidebar/recent_comments');

  $this->end();


 ・ブロックに追記

  $this->append('sidebar');

  echo $this->element('sidebar/popular_topics');

  $this-end();

  // 上と同じ

  $this->append('sidebar', $this->element('sidebar/popular_topics'));


 ・ブロックの消去

  $this->reset('sidebar');

  $this->assign('sidebar', '');


 ・ブロックへのコンテンツ代入

  $this->assign('title', $title);


 ・ブロックの前にコンテンツ追加

  $this->prepend('sidebar', 'this contents will put before sidebar');


 ・ブロックの表示

  // menuブロックが存在する場合に表示する

  <?php if ($this->fetch('menu')): ?>

  <div class="menu">

   <h3>Menu options</h3>

   <?= $this->fetch('menu') ?>

  </div>

  </php endif; ?>

  // cartブロックが存在しない場合、規定値を表示する

  <?= $this->fetch('cart', 'shopping cart is empty') ?>


・scriptとcssのためのブロック使用

  // ビューファイルの中で

  <?php

   $this->Html->script('carousel', ['block' => true]);

   $this->Html->css('carousel', ['block' => true]);

  ?>

  // レイアウトファイルの中で

  <html>

  <head>

   <title><?= $this->fetch('title') ?><?title>

   <?= $this->fetch('script') ?>

   <?= $this->fetch('css') ?>


・レイアウト

 レイアウトの中にビューが表示される。

 規定のレイアウトは、src/Template/Layout/default.ctp


 ビューの内容を表示する場所に、

  <?= $this->fetch('content') ?>

 を入れる。


 ・レイアウトの切り替え

  レイアウトは、src/Template/Layoutディレクトリに置く。

  // コントローラから切り替え

  public function admin_view() {

   $this->viewBuilder()->setLayout('admin');

  }

  // ビューファイルから切り替え

  $this->layout = 'loggedin';


・エレメント

 ・エレメントの作成

  再利用が可能なパーツ(小さなビュー)

  ヘルプ、ナビゲーションコントロール、追加メニュー、ログインフォーム等。

  src/Template/Element フォルダに .ctp拡張子で置く。


 ・エレメントの出力

  // ビューファイルで

  echo $this->element('helpbox');


 ・エレメントへ引数を渡す

  // エレメントファイルで

  echo $helptext;

  // ビューファイルで

  echo $this->element('helpbox', ["helptext" => "This is Help."]);


・ビュークラス

 ・ビュークラスの作成

  src/View に配置する。

   src/View/PdfView.php


  Viewをサフィックスとしてつける。

   PdfView


  ビュークラスを参照するときはViewサフィックスを省略する。

   $this->viewBuilder()->className('Pdf');


  Viewを継承する。

   class PdfView extends View