将数据从我的控制器代码点火器传递到我的视图中


Passing data into my view from my controller codeigniter

我已经为我的项目构建了一个布局模板(请参阅布局函数)。模板系统将页眉、页脚和左侧添加到页面,还将当前视图呈现为"middle"

问题是我无法正确包含$data变量。这是为了让我的数据打印出来,我需要像选项 1 中的波纹管一样包含它。

选项 1

  $this->load->view('profile_view', $data);

布局功能

                public function layout () {
                $this->template['header'] = $this->load->view('layout/header', $this->Front_End_data, true);
                $this->template['left'] = $this->load->view('layout/left', $this->Front_End_data, true);
                $this->template['middle'] = $this->load->view($this->middle, $this->Front_End_data, true);
                $this->template['footer'] = $this->load->view('layout/footer', $this->Front_End_data, true);
                $this->load->view('layout/index', $this->template);

这工作渲染模板,但我需要包含$data

function index()
{
    $data = array();
    $this->load->model('user_profile/profiles_model');
    $query = $this->profiles_model->get_bank();
    if (!empty($query)) {
        $data['records'] = $query;
    }
    $this->middle = 'profile_view';
    $this->layout();
}    

最好的解决方案是什么?

更新

我尝试过一些似乎部分工作的东西,但复制了两次视图,一次在正确的地方,一次错了,知道吗?

function index()
    {
        $data = array();
        $this->load->model('user_profile/profiles_model');
        $query = $this->profiles_model->get_bank();
        if (!empty($query)) {
            $data['records'] = $query;
        }
//        $this->middle = 'profile_view';
        $this->load->view($this->middle = 'profile_view',$data);
        $this->layout();
//        $this->layout($data);
    }

更新

这也有效,但不是最好的方法

function index()
    {
        $data = array();
        $this->load->model('user_profile/profiles_model');
        $query = $this->profiles_model->get_bank();
        if (!empty($query)) {
            $data['records'] = $query;
        }
//        $this->load->view($this->middle = 'profile_view',$data);
        $this->template['middle'] = $this->load->view ($this->middle = 'profile_view',$data, true);
        $this->layout();
//        $this->middle = 'profile_view';
//        $this->layout($data);
    }

为什么不简单地将$data传递给布局方法?

    $this->layout($data);

使用这个由 Phil Sturgeon 创建的库。它很棒,非常易于设置和使用。我用编码点火器构建的任何项目,这是我添加的第一件事。当已经有飞机可用时,为什么要重新创建轮子?

它不优雅,但这是我修复它的方法

$this->template['middle'] = $this->load->view ($this->middle = 'pages/update_view',$data, true);
        $this->layout();