CodeIgniter干导航 [英] CodeIgniter Dry Navigation
本文介绍了CodeIgniter干导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的老板告诉我做干燥导航不要使用重复的代码,对于导航我试图扩展CI_Controler和构造加载头nav,body,footer文件。
我的问题是当我创建新的控制器,当我尝试加载不同的视图文件,如何achive的
我的扩展控制器
class MY_Controller extends CI_Controller
{
public function __construct(){
parent :: __ construct ;
$ this-> load-> view('view_header');
$ this-> load-> view('includes / nav_home');
$ this-> load-> view('view_home');
$ this-> load-> view('view_footer');
}
}
控制器如何加载diffrent视图文件
class Pages extends MY_Controller
{
public function __construct
$ this-> load-> view('includes / nav_new_view');
}
}
方案
您可以自己创建一个模板库。例如:
函数模板($ view_name,$ view_data){
//下面将从视图返回html字符串name
$ data ['content'] = $ this-> load-> view($ view_name,$ view_data,true)
//加载主模板视图并传递html string to main template
$ this-> load-> view('main_template',$ data);
}
在主模板中,只是echo $ content
My boss told me make dry navigation dont use repetitive code, for navigation i am trying to extend CI_Controler and with construct load header nav, body, footer files.
My question is when i create new controller and when i try to load different view files, how to achive that???
my extended controler
class MY_Controller extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->view('view_header');
$this->load->view('includes/nav_home');
$this->load->view('view_home');
$this->load->view('view_footer');
}
}
and later when i am creating new controler how to load diffrent view files
class Pages extends MY_Controller
{
public function __construct() {
$this->load->view('includes/nav_new_view');
$
}
}
解决方案
You can create a template library yourself. For example :
function template($view_name,$view_data){
//below will return html string from view name
$data['content'] = $this->load->view($view_name,$view_data,true)
//load main template view and pass the html string to main template
$this->load->view('main_template',$data);
}
In main template, just echo $content
这篇关于CodeIgniter干导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文