我将函数_homepage传递给索引但它没有传递$ data ['文章']。 任何人都可以帮我解决这个问题吗? 此代码已更新
public function index(){
$mydata = array();
$this->mydata['menu'] = $this->Mdl_page->get_nested();
$this->mydata['page'] = $this->Mdl_page->get_by(array('slug' => (string) $this->uri->segment(1)), TRUE);
count($this->mydata['page']) || show_404(current_url());
//fetch page data
$method = '_' . $this->mydata['page']->template;
if (method_exists($this, $method)) {
$this->$method();
}
else {
log_message('error', 'Could not load template ' . $method .' in file ' . __FILE__ . ' at line ' . __LINE__);
show_error('Could not load template ' . $method);
}
$this->mydata['subview'] = $data['page']->template;
//dump($data);
$this->load->view('_main_layout', $this->mydata);
}
private function _page(){
dump('Welcome from the page template');
}
private function _homepage(){
$this->load->model('admin/Mdl_Articles');
$this->db->limit(6);
$this->mydata['articles'] = $this->Mdl_Articles->get();
//dump($data['articles']);
}
private function _news_archive(){
dump('Welcome from the newspage template');
}
答案 0 :(得分:0)
您应该创建一个私有数组属性
private mydata = array();
在_homepage
方法中,内容应为:
private function _homepage(){
// ...
// target your class scope property
$this->mydata['articles'] = $this->Mdl_Articles->get();
并在$data
方法中将所有$this->mydata
变量更改为index
,并将其传递给视图,如下所示:
$this->load->view('_main_layout', $this->mydata);