一部のActionのみビューを適用しない

概要

一部のページのみテンプレートを使用しない

各ActionよりResponseオブジェクトを返却した場合のみテンプレートを無効にして固有のビューのみ使用できる


コントローラーはController_Templateを継承

app/classes/controller/sample08.php

<?php
 
// サンプルコントローラー(テンプレート)
class Controller_Sample08 extends Controller_Template
{
	// テンプレートファイルの情報
	public $template='template';		// app/views/template.phpの場合
 
 
	// action前の処理を記述
	public function before()
	{
		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('sample08/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('sample08/hoge1', $data);
	}
 
	// アクションhoge2の画面
	public function action_hoge2()
	{
		// actionの固有データ初期化
		$data = array();
		$data["title"] = "hoge2のタイトル";
		$data["detail"] = "hoge2のアクションページ";
 
		// Responseで返却された場合はテンプレート未使用となる
		return new Response(View::forge('sample08/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/sample08/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

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title><?= $title ?></title>
	</head>
	<body>
		<div>
			<?php echo $title; ?>
		</div>
		<div>
			今回はテンプレート未使用にしな場合のページ
		</div>
	</body>
</html>
 
 

確認

index.php

hoge1.php

hoge2.php




最終更新:2013年05月22日 23:44