How to pass data into view file

ε祈祈猫儿з 提交于 2020-01-11 13:24:10

问题


My core class

class MY_Loader extends CI_Loader
{
    public function template($template_name, $vars = array(), $return = FALSE)
    {
        if($return):
            $content  = $this->view('admin/common/header', $vars, $return);
            $content  = $this->view('admin/common/leftmenu', $vars, $return);
            $content  = $this->view('admin/common/topbar', $vars, $return);
            $content .= $this->view($template_name, $vars, $return);
            $content .= $this->view('admin/common/footer', $vars, $return);
            return $content;
        else:
            $this->view('admin/common/header', $vars);
            $this->view('admin/common/leftmenu', $vars);
            $this->view('admin/common/topbar', $vars);
            $this->view($template_name, $vars);
            $this->view('admin/common/footer', $vars);
        endif;
    }
}

My function

public function index()
{
    $title = "Hello Admin";
    $this->load->template('admin/cmspages', $title);
}

<div class="container-fluid">
      <!-- Page Heading -->
      <h1 class="h3 mb-4 text-gray-800">Blank Page </h1>
      <?php echo $title ; ?>
</div>

i made common header footer and sidebar using ci_loader in core folder but unable to paas value in view page what i have to do?

i have tried these code please explain where i am wrong


回答1:


Your template method (and CI view method) awaits $vars as array, but you are passing only string.

Correct usage would be:

public function index() {
    $data = array(
        'title' => 'Hello Admin',
    );
    $this->load->template('admin/cmspages', $data);
}


来源:https://stackoverflow.com/questions/58098399/how-to-pass-data-into-view-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!