Template

概要

共通のテンプレートレイアウトを使用してレイアウトの共通化を行う機能。

この機能により、似たようなページを毎回コントローラーで制御することが不要になる


イメージ


使用方法

テンプレート用のビューファイルを用意

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>
 
 

個別Action用のビューファイルを用意

app/views/sample06/index.php
<div>
	<?php echo $title; ?>
</div>
<div>
	ここは固有action[<?php echo $detail; ?>]です
</div>
 
 
app/views/sample06/hoge1.php
<div>
	<?php echo $title; ?>
</div>
<div>
	ここは固有action[<?php echo $detail; ?>]です
</div>
<span>テストaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
 
 
app/views/sample06/hoge2.php
<div>
	<?php echo $title; ?>
</div>
<div>
	ここは固有action[<?php echo $detail; ?>]です
</div>
<span>テストbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb</span>
 
 

コントローラーにテンプレートと固有の処理を記述

app/classes/controller/sample06.php
<?php
 
// サンプルコントローラー(テンプレート)
class Controller_Sample06 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('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);
	}
 
 
}
 
 

確認

indexの場合

hoge1の場合

hoge2の場合


テンプレートサンプル

ベーシックサンプル

一部のactionのみテンプレートを変更

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




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