CakePHP2.xでタイトル、ディスクリプションに変数(文字列)をページ毎に送る方法
viewファイルで、
<?php $post = "タイトル用実験"; $this->assign('title_for_layout', $post); $this->assign('after_sentence', 'のページです'); $this->assign('discription_for_layout', 'ページです');
このように、title_for_layout、after_sentence、discription_for_layout を設定します。
app⇒view⇒Layout⇒default.ctp ファイルのタイトルタグのあたりに
<?php if($this->fetch('title_for_layout') != null){ ?> <title> <?php echo $this->fetch('title_for_layout'); ?><?php echo $this->fetch('after_sentence').' | サイト名'; ?> </title> <meta name="description" content = <?php echo $this->fetch('title_for_layout').'の'.$this->fetch('discription_for_layout') ?> > <?php }else { ?> <title> サイト名 | サイトの説明など </title> <meta name="description" content= <?php echo $this->fetch('discription_for_layout') ?> > <?php } ?>
このように ビューファイルでは
$this->assign('変数名', $変数);
defaultファイルでは
$this->fetch('変数名');
で、ビューからの変数(文字列)を受信できるので、ページごとに通るようなフォーマットを作り、タイトルとディスクリプションに使いましょう。
以上です。