複数の1つのビューファイルから別のビューファイルを呼び出して展開(遅延レンダリング)

概要

コントローラーで複数のビューを読み込んで1つのビューに展開する

変数にデータを設定しておいて、あとでまとめてレンダリングする


コントローラー定義

<?php
 
// サンプルコントローラー
class Controller_Sample02 extends Controller
{
	// アクションを省略時のデフォルトの画面
	public function action_index()
	{
		//ビューを作成
		$view = View::forge('sample02/index');
 
		// 全てのビューがアクセスできるように設定
		$view->set_global('username', 'ほげほげ04');
		$view->set_global('title', 'サンプルテスト(ビュー4)');
		$view->set_global('site_title', 'Test Site');
 
		// 変数としてビューを割り当てる
		$view->head = View::forge('sample02/head');
		$view->header = View::forge('sample02/header');
		$view->content = View::forge('sample02/content');
		$view->footer = View::forge('sample02/footer');
 
		// ビューをブラウザに出力
		return $view;
	}
 
}
 
 
 

ビューファイル定義

index.php

<!DOCTYPE html>
<html>
	<head>
		<?php echo $head; ?>
	</head>
	<body>
		<?php echo $header; ?>
		<?php echo $content; ?>
		<?php echo $footer; ?>
	</body>
</html>
 
 

head.php

<meta charset="utf-8">
<title><?php echo $title; ?></title>
 
 

header.php

<div class="logo"></div>
<div class="logo_text">
	<?php echo $site_title; ?>
</div>
 
 

content.php

<title><?php echo $title; ?></title>
<div class="welcome_user">ようこそ <?php echo $username; ?></div>
 
 

footer.php

<div class="footer">
&copy; Copyright<?=date('Y')?><?php echo $site_title; ?>
</div>
 
 



最終更新:2013年05月23日 00:14