【问题标题】:How to dynamically load multiple views in CI 3.0.x如何在 CI 3.0.x 中动态加载多个视图
【发布时间】:2016-02-24 16:20:16
【问题描述】:

我正在尝试在 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

任何想法为什么?

【问题讨论】:

    标签: php model-view-controller codeigniter-3


    【解决方案1】:

    想通了。在 My_controller 中,我没有意识到可以将视图连接为字符串。

    所以我把它改成了:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-07
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      相关资源
      最近更新 更多