CodeIgniter / PHP - calling a view from a view

Mostly for my webapp, I try to organize it a little better. Since at the moment, every time I want to load a page, I have to do it from my controller as follows:

$this->load->view('subviews/template/headerview'); $this->load->view('subviews/template/menuview'); $this->load->view('The-View-I-Want-To-Load'); $this->load->view('subviews/template/sidebar'); $this->load->view('subviews/template/footerview'); 

As you can say, this is not very effective.

So, I thought that I was creating one β€œmain” view - it is called template.php. This is the contents of the template:

 <?php $view = $data['view']; $this->load->view('subviews/template/headerview'); $this->load->view('subviews/template/menuview'); $this->load->view($view); $this->load->view('subviews/template/sidebar'); $this->load->view('subviews/template/footerview'); ?> 

And then I thought that I could call it from the controller as follows:

  $data['view'] = 'homecontent'; $this->load->view('template',$data); 

Unfortunately, I just can't do this job. Does anyone have any ways to do this or fixes that I can create? I tried putting "and" around $ view in template.php, but that doesn't make any difference. Common error: "Undefined variable: data" or "Can not load view: $ view.php", etc.

Thanks guys!

Jack

+4
source share
2 answers

I believe you have:

 $view = $data['view']; $this->load->view('subviews/template/headerview'); $this->load->view('subviews/template/menuview'); $this->load->view($view); $this->load->view('subviews/template/sidebar'); $this->load->view('subviews/template/footerview'); 

You just need to get rid of the line:

 $view = $data['view']; 

This is due to the fact that when an array is transferred from the controller, the variable in the view can be obtained simply using $ view, not $ data ['view'].

+13
source

A few suggestions here http://codeigniter.com/forums/viewthread/88335/

I chose this method: Controller class:

 public function __construct() { parent::__construct(); $this->load->vars(array( 'header' => 'partials/header', 'footer' => 'partials/footer', )); } public function index() { $data['page_title'] = 'Page specific title'; $this->load->view('my-view', $data); } 

View:

 <?php $this->load->view($header, compact('page_title')); ?> ... blah blah ... <?php $this->load->view($footer); ?> 

Acquiring a representation in a representation and passing on any variables that can be used by your children's representation is far from ideal. Being able to use something like Action Filters in Laravel would probably be better.

+6
source

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


All Articles