symfonyのinclude_component()見たいな仕組みを作る
もとのviewだけでやるパターンだとcontrollerが今から作るページに乗せるデータを全て知っていなければならない。これはページのフラグメントを作ってページ本体に組み込むのに都合が悪い。
だからこんな事になった。
helperに"include_component()"を追加(components_helper.php)
if ( ! function_exists('include_component')) { function include_component($component = '',$data = null) { $CI=& get_instance(); $CI->load->library($component); $objname = str_replace("/","_", $component); $CI->$objname->execute($data); } }
objnameの置換はhttp://d.hatena.ne.jp/KuniTsuji/20080616/1213602119:この辺のLoaderを使わせていただいたのでこうなった。library/hoge/huga.phpにコンポーネント化するファイルを置ける。
componentを作る
<?php class components_Testblock{ function Testblock(){ } function execute($data=null){ $CI=& get_instance(); //この辺でモデル操作とか色々して$dataにセット。 $CI->load->view("blockview",$data); } } ?>
自分(コンポーネント自体)が把握するviewにデータをアサイン。
大元のview
<?php $this->load->helper("components");?> <?php include_component("components/testblock",array("title" => $title)) ; ?>
引数に"title"って入れればblockview.phpは$titleを使える。
<h3><?php echo $title ?></h3>
とか。
けど面倒くさい。
というかviewで$thisをよーく見たら"CI_Loader"だったよ!なんでだよ!