📜 ⬆️ ⬇️

Solving the problem of lack of layout in codeigniter

A known problem with this framework is the lack of a built-in library of layouts. This seriously limits the development of web applications, even medium level of complexity. The page is called as a composite pattern:
 $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');

It is clear that such a record is cumbersome and inconvenient. It would be much more convenient to call the view as a nested template - once:

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


Recently, I came across an overseas article with an example solving this problem. The principle is quite simple. The application / libraries / my_layout.php library is being created.

 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); } } }

')
In the controller used, it is enough to connect our library and access the required view via $ 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); } }


You can also transmit several types at the same time:

 $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);


And finally, you can turn off part of your layout or use a different view instead.

 $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;


Before that, I used my own library of layouts, but this solution seemed more interesting and simpler to me. I hope the article will be useful.

Source: https://habr.com/ru/post/139875/


All Articles