在Codeigniter的所有控制器上具有固定的视图 [英] having a fixed view across all controllers in Codeigniter
问题描述
为了加载CodeIgniter中的视图,我必须重复加载固定的视图(页眉和页脚),这对于每个视图相关的控制器都是重复的。
目前,当我要在CI中加载视图时,我执行以下操作:
$ this-> load-> ; view(header);
$ this-> load-> view(index);
$ this-> load-> view(footer);
然后,如何更改 $ this-> load-> view();
来获取一个参数(例如boolean),它允许在目标视图之前/之后加载视图。例如:
$ this-> load-> view(index,TRUE,FALSE,$ data ); // TRUE => header FALSE => footer $ data =>公用变量
可能破解这样的函数?
您可以使用 library
。
创建一个名为 template.php
的新库文件并编写一个名为 load_template
。
public function load_template($ view_file_name,$ data_array = array())
{ / p>
$ ci =& get_instatnce();
$ ci-> load-> view(header);
$ ci-> > view($ view_file_name,$ data_array);
$ ci - >> load-> view(footer);
}
您必须在自动载入档夹。因此您不想在所有控制器中加载。
您可以使用
$ this-> template-> load_template(index);
如果要将日期传递给查看文件,那么您可以通过 $ data_array
发送
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?
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
这篇关于在Codeigniter的所有控制器上具有固定的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!