如何在CI 3.0.x中动态加载多个视图
How to dynamically load multiple views in CI 3.0.x
我正在尝试在 CI 3 中动态加载多个视图。但是我遇到了一些挑战。我扩展了处理布局的 CI_controller:
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
.
知道为什么吗?
想通了。在 My_controller 中,我没有意识到视图可以连接为一个字符串。
所以我将其更改为:
$this->template['page_content'] = '';
foreach ($this->page_content as $content) {
$this->template['page_content'] .= $this->load->view($content, $this->data, true);
}
我正在尝试在 CI 3 中动态加载多个视图。但是我遇到了一些挑战。我扩展了处理布局的 CI_controller:
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
.
知道为什么吗?
想通了。在 My_controller 中,我没有意识到视图可以连接为一个字符串。
所以我将其更改为:
$this->template['page_content'] = '';
foreach ($this->page_content as $content) {
$this->template['page_content'] .= $this->load->view($content, $this->data, true);
}