codeigniterのレイアウトの不足を解決する

このフレームワークの既知の問題は、レイアウトの組み込みライブラリが不足していることです。 これにより、中程度の難易度であっても、Webアプリケーションの開発が大幅に制限されます。 ページは複合テンプレートとして呼び出されます。

 $data['page_title'] = 'Your title'; $this->load->view('header'); $this->load->view('menu'); $this->load->view('content', $data); $this->load->view('footer');
      
      









$data['page_title'] = 'Your title'; $this->load->view('header'); $this->load->view('menu'); $this->load->view('content', $data); $this->load->view('footer');











そのような録音が面倒で不便であることは明らかです。 ビューをネストされたテンプレートとして呼び出す方がはるかに便利です-1回:



 $this->load->view('content', $data); 
      









$this->load->view('content', $data);













私は最近、この問題を解決する例がある海外の記事に出会いました。 原理は非常に簡単です。 ライブラリアプリケーション/ libraries / my_layout.phpが作成されます



 class MY_Layout extends CI_Controller { //     public $header = 'header'; public $footer = 'footer'; //      :       public function content($views = '', $data = '') { //  header if ($this->header) { $this->load->view($this->header, $data); } //   ,       if (is_array($views)) { foreach ($views as $view) { $this->load->view($view, $data); } } else { $this->load->view($views, $data); } //  footer if ($this->footer) { $this->load->view($this->footer); } } }
      
      









class MY_Layout extends CI_Controller { // public $header = 'header'; public $footer = 'footer'; // : public function content($views = '', $data = '') { // header if ($this->header) { $this->load->view($this->header, $data); } // , if (is_array($views)) { foreach ($views as $view) { $this->load->view($view, $data); } } else { $this->load->view($views, $data); } // footer if ($this->footer) { $this->load->view($this->footer); } } }













使用するコントローラーでは、ライブラリを接続し、$ this-> my_layout-> content( 'user / test'、$ data)を使用して目的のビューに切り替えるだけで十分です。



 class User extends CI_Controller { public function __construct() { parent::__construct(); $this->load->library('MY_Layout'); } public function test() { $data['title'] = 'dynamic_string'; $this->my_layout->content('user/test', $data); } }
      
      









class User extends CI_Controller { public function __construct() { parent::__construct(); $this->load->library('MY_Layout'); } public function test() { $data['title'] = 'dynamic_string'; $this->my_layout->content('user/test', $data); } }













複数のタイプを同時に転送することもできます。



 $data['title'] = 'dynamic_string'; $views = array( 'menu' => 'menu', 'content' => 'user/test' ); $this->my_layout->content($views, $data);
      
      









$data['title'] = 'dynamic_string'; $views = array( 'menu' => 'menu', 'content' => 'user/test' ); $this->my_layout->content($views, $data);













最後に、レイアウトの一部をオフにするか、代わりに別のビューを使用できます。



 $this->my_layout->header = 'user/custom_user_header'; // or turn off header $this->my_layout->header = FALSE;
      
      









$this->my_layout->header = 'user/custom_user_header'; // or turn off header $this->my_layout->header = FALSE;













それ以前は、レイアウトのライブラリを使用していましたが、このソリューションはより面白く、よりシンプルに思えました。 この記事がお役に立てば幸いです。



All Articles