2016-02-24 52 views
0

我試圖在CI 3中動態加載多個視圖。但是我遇到了一些挑戰。我已經延長處理該佈局是CI_Controller:如何在CI 3.0.x中動態加載多個視圖

public function layout() { 
    $this->template['page_header'] = $this->load->view('global/header', $this->data, true); 
    if (is_array($this->page_content)) { 
     foreach ($this->page_content as $key => $value) { 
     $this->template['page_content'][$key] = $this->load->view($value, $this->data, true); 
     } 
    } 
    else { 
     $this->template['page_content'] = $this->load->view($this->page_content, $this->data, true); 
    } 
    $this->template['page_footer'] = $this->load->view('global/footer', $this->data, true); 
    $this->load->view('global/layout', $this->template); 
    } 

在延伸MY_Controller控制器,我有:

$this->page_content = array('cards/cards3', 'cards/cards1', 'cards/cards2'); 
$this->layout(); 

在視圖我有:

if (is_array($page_content)) { 
    foreach ($page_content as $content) { 
     echo $content; 
    } 
}  
else { 
    echo $page_content; 
} 

的問題是在視圖中我得到了數組中的最後一項。在這種情況下,cards/cards2

任何想法爲什麼?

回答

0

想通了。在My_controller中,我沒有意識到視圖可以串接爲一個字符串。

,所以我把它改爲:

$this->template['page_content'] = ''; 
foreach ($this->page_content as $content) { 
    $this->template['page_content'] .= $this->load->view($content, $this->data, true); 
}