2012/05/24

共通ヘッダー フッター(HTML) →その後


結局 hookは諦めて、Controllerを拡張して、loadクラスの なんちゃってview()みたいなのを作って落ち着いた。
動的に、headerのtitleを変えるにはこれしかなかったにょん(´・ω・`)ガッカリ…
app/core/MY_Controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script accessallowed');
class MY_Controller extends CI_Controller {
  private $vars = array();

  /**
   * Constructor
   */
  public function __construct()
  {
    parent::__construct();
  }

  /**
   * set vars
   * @access public
   * @param  $key  string
   * @param  $val  string
   * @return 
   */
  public function setVars($key, $val)
  {
    $this->vars[$key] = $val;
  }

  /**
   * set views
   * @access public
   * @param  $view  mixed
   * @param  $return bool
   * @return void
   */
  public function setView($view, $return = FALSE)
  {
    // common header
    $this->load->view('parts/header', $this->vars);
    if (is_array($view))
    {
      foreach ($view as $k) $this->load->view($k, $this->vars);
    }
    else
    {
      $this->load->view($view, $this->vars);
    }
    // common footer
    $this->load->view('parts/footer', $this->vars);
  }
}
app/views/parts/header.php
…略
<title>My Site Title - <?=$title?></title>
…略
・呼び出しコントローラ
app/controllers/hoge.php
<?php if ( ! defined('BASEPATH')) exit('No direct script accessallowed');
class Hoge extends MY_Controller {

  /**
   * list
   */
  public function list()
  {
    $this->setVars('title', 'HOGE LIST');
    $this->setView('hoge/list');
  }

  /**
   * edit
   */
  public function edit()
  {
    $this->setVars('title', 'HOGE EDIT');
    $this->setView('hoge/edit');
  }
}




0 件のコメント: