having a fixed view across all controllers in Code

2019-02-27 06:16发布

问题:

For loading the views in CodeIgniter, I have to repeat loading the fixed views (header and footer) which is a little annoying to be repeated for every view-related controller.

Currently when I want to load views in CI, I do the following:

$this->load->view("header");
$this->load->view("index");
$this->load->view("footer");

Then, how can I change $this->load->view(); to get a parameter (for instance boolean) which allows a view to be loaded before/after the targeted view. Such as this one:

$this->load->view("index", TRUE, FALSE, $data); // TRUE=>header  FALSE=>footer $data=>common variable

Is it possible to hack the function like this?

回答1:

You can do with library.

Create a new library file called template.php and write a function called load_template. In that function, use above code.

public function load_template($view_file_name,$data_array=array()) {

$ci = &get_instatnce();

$ci->load->view("header");

$ci->load->view($view_file_name,$data_array);

$ci->> load->view("footer");

}

You have to load this library in autoload file in config folder. so you don't want to load in all controller.

You can this function like

$this->template->load_template("index");

If you want to pass date to view file, then you can send via $data_array



回答2:

try this library, it worked for me, when I used it

https://github.com/philsturgeon/codeigniter-template