複数の1つのビューファイルから別のビューファイルを呼び出して展開(強制レンダリング)
概要
コントローラーで複数のビューを読み込んで1つのビューに展開する
各ビューのデータをコントローラー内部でレンダリングしたデータをメインビューに設定するのでタグなどが含まれる場合はエスケープ解除することも必要
コントローラー定義
<?php
// サンプルコントローラー
class Controller_Sample03 extends Controller
{
// アクションを省略時のデフォルトの画面
public function action_index()
{
//変数を割り当てる
$data = array();
$data['username'] = 'ほげほげ05';
$data['title'] = 'サンプルテスト(ビュー5)';
$data['site_title'] = 'Test Site';
//変数としてビューを割り当てる、強制レンダリング
$views = array();
$views['head'] = View::forge('sample03/head', $data)->render();
$views['header'] = View::forge('sample03/header', $data)->render();
$views['content'] = View::forge('sample03/content', $data)->render();
$views['footer'] = View::forge('sample03/footer', $data)->render();
// レンダリングした HTML をリクエストに返す
return View::forge('sample03/index', $views, false)->render();
}
}
ビューファイル定義
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">
© Copyright<?=date('Y')?><?php echo $site_title; ?>
</div>
最終更新:2013年05月23日 00:14