如何将数据传递到视图文件
How to pass data into view file
我的核心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;
}
}
我的函数
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>
我在核心文件夹中使用 ci_loader 制作了通用的页眉页脚和侧边栏,但无法在视图页面中传递值我必须做什么?
我试过这些代码请解释我哪里错了
您的 template
方法(和 CI view
方法)等待 $vars
作为数组,但您只传递字符串。
正确的用法是:
public function index() {
$data = array(
'title' => 'Hello Admin',
);
$this->load->template('admin/cmspages', $data);
}
我的核心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;
}
}
我的函数
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>
我在核心文件夹中使用 ci_loader 制作了通用的页眉页脚和侧边栏,但无法在视图页面中传递值我必须做什么?
我试过这些代码请解释我哪里错了
您的 template
方法(和 CI view
方法)等待 $vars
作为数组,但您只传递字符串。
正确的用法是:
public function index() {
$data = array(
'title' => 'Hello Admin',
);
$this->load->template('admin/cmspages', $data);
}