"best method of including views within views in codeigniter" Answer’s


Views within other views are called Nested views. There are two ways of including nested views in CodeIgniter:

1. Load a nested view inside the controller

Load the view in advance and pass to the other view. First put this in the controller:

// the "TRUE" argument tells it to return the content, rather than display it immediately
$data['menu'] = $this->load->view('menu', NULL, TRUE);
$this->load->view ('home', $data);

Then put <?=$menu?> in your view at the point you want the menu to appear.

2. Load a view "from within" a view

First put this in the controller:


Then put this in the /application/views/home.php view:

<?php $this->view('menu'); ?>

<p>Other home content...</p>

About best method, I prefer the 1st method over 2nd one, because by using 1st method I don't have to mix up code, it is not like include php. Although indirectly both are same, the 1st method is clearer & cleaner than 2nd one!

Wednesday, March 31, 2021
answered 11 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :