一部のactionのみテンプレートを変更
概要
デフォルトで使用するテンプレートとは別に指定のactionのみテンプレートを別のものへ切り替える
beforeメソッドでテンプレートプロパティを変更することで対応可能
コントローラーはController_Templateを継承
app/classes/controller/sample07.php
<?php
// サンプルコントローラー(テンプレート)
class Controller_Sample07 extends Controller_Template
{
// テンプレートファイルの情報
public $template='template'; // app/views/template.phpの場合
// action前の処理を記述
public function before()
{
// 指定のActionの場合にテンプレートを切り替え
if(Request::active()->action == "hoge1"){
$this->template='template_hoge1';
}
parent::before();
}
// action後の処理を記述
public function after($response)
{
$response = parent::after($response);
return $response;
}
// アクションを省略時のデフォルトの画面
public function action_index()
{
// actionの固有データ初期化
$data = array();
$data["title"] = "indexのタイトル";
$data["detail"] = "indexのアクションページ";
// テンプレートに割り当てるデータを設定
$this->template->title = 'サンプル';
$this->template->content = View::forge('sample06/index', $data);
}
// アクションhoge1の画面
public function action_hoge1()
{
// actionの固有データ初期化
$data = array();
$data["title"] = "hoge1のタイトル";
$data["detail"] = "hoge1のアクションページ";
// テンプレートに割り当てるデータを設定
$this->template->title = 'ほげ1';
$this->template->content = View::forge('sample06/hoge1', $data);
}
// アクションhoge2の画面
public function action_hoge2()
{
// actionの固有データ初期化
$data = array();
$data["title"] = "hoge2のタイトル";
$data["detail"] = "hoge2のアクションページ";
// テンプレートに割り当てるデータを設定
$this->template->title = 'ほげ2';
$this->template->content = View::forge('sample06/hoge2', $data);
}
}
テンプレートレイアウトはviews直下
app/views/template.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
</head>
<body>
<div id="wrapper">
<h1><?php echo $title; ?></h1>
<div id="content">
<?php echo $content; ?>
</div>
</div>
</body>
</html>
app/views/template_hoge1.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
</head>
<body>
<div id="wrapper">
<h1><?php echo $title; ?></h1>
<span style="color: red;font-weight: bold;">このテンプレートはhoge1のものです</span>
<div id="content">
<?php echo $content; ?>
</div>
</div>
</body>
</html>
個別のレイアウトは各ディレクトリ直下
app/views/sample07/index.php
<div>
<?php echo $title; ?>
</div>
<div>
ここは固有action[<?php echo $detail; ?>]です
</div>
app/views/sample07/hoge1.php
<div>
<?php echo $title; ?>
</div>
<div>
ここは固有action[<?php echo $detail; ?>]です
</div>
<span>テストaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
app/views/sample07/hoge2.php
<div>
<?php echo $title; ?>
</div>
<div>
ここは固有action[<?php echo $detail; ?>]です
</div>
<span>テストbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb</span>
確認
index.php
hoge1.php
hoge2.php
最終更新:2013年05月22日 23:43