I am trying to load multiple views dynamically in CI 3. But I am having some challenges. I have extended the CI_controller that handles the layout:
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);
}
In the controller that extends MY_Controller, I have:
$this->page_content = array('cards/cards3', 'cards/cards1', 'cards/cards2');
$this->layout();
In the view I have:
if (is_array($page_content)) {
foreach ($page_content as $content) {
echo $content;
}
}
else {
echo $page_content;
}
The problem is that on the view I am getting the last item in the array. In this case, cards/cards2
.
Any ideas why?
Figured it out. In My_controller i did not realize the views can be concatenated as a string.
SO i changed it to:
$this->template['page_content'] = '';
foreach ($this->page_content as $content) {
$this->template['page_content'] .= $this->load->view($content, $this->data, true);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.