CodeIgniter/PHP-从视图中调用视图

CodeIgniter/PHP-从视图中调用视图,php,codeigniter,views,Php,Codeigniter,Views,基本上,对于我的webapp,我正试图把它组织得更好一些。目前,每当我想加载一个页面时,我都必须从我的控制器上这样做: $this->load->view('subviews/template/headerview'); $this->load->view('subviews/template/menuview'); $this->load->view('The-View-I-Want-To-Load'); $this-

基本上,对于我的webapp,我正试图把它组织得更好一些。目前,每当我想加载一个页面时,我都必须从我的控制器上这样做:

        $this->load->view('subviews/template/headerview');
    $this->load->view('subviews/template/menuview');
    $this->load->view('The-View-I-Want-To-Load');
    $this->load->view('subviews/template/sidebar');
    $this->load->view('subviews/template/footerview'); 
正如你所看到的,这并不是很有效

所以我想我应该创建一个“主”视图——它叫做template.php。这是模板视图的内容:

<?php
    $view = $data['view'];

        $this->load->view('subviews/template/headerview');
        $this->load->view('subviews/template/menuview');
        $this->load->view($view);
        $this->load->view('subviews/template/sidebar');
        $this->load->view('subviews/template/footerview');
?>
不幸的是,我根本无法做到这一点。有没有人有办法解决这个问题,或者我可以进行修复?我已经尝试在template.php中放置大约$view的“s”和“s”,但这没有什么区别。通常的错误是“未定义变量:数据”或“无法加载视图:$view.php”等

谢谢大家


杰克

我相信你有:

$view = $data['view'];

$this->load->view('subviews/template/headerview');
$this->load->view('subviews/template/menuview');
$this->load->view($view);
$this->load->view('subviews/template/sidebar');
$this->load->view('subviews/template/footerview');
你只需要摆脱这一行:

$view = $data['view'];

这是因为当从控制器传递数组时,视图上的变量可以通过$view而不是$data['view']访问。

这里有很多建议

我选择了这个方法: 控制器类:

public function __construct() 
{
    parent::__construct();

    $this->load->vars(array(
        'header' => 'partials/header',
        'footer' => 'partials/footer',
    ));
}

public function index()
{       
    $data['page_title'] = 'Page specific title';        
    $this->load->view('my-view', $data);
}
视图:


... 胡说八道。。。

必须在视图中加载视图并传递您的子视图可能使用的任何变量,这远远不够理想。能够使用类似的东西可能会更好。

谢谢,我会试试的!我几分钟后回来报告。
public function __construct() 
{
    parent::__construct();

    $this->load->vars(array(
        'header' => 'partials/header',
        'footer' => 'partials/footer',
    ));
}

public function index()
{       
    $data['page_title'] = 'Page specific title';        
    $this->load->view('my-view', $data);
}
<?php $this->load->view($header, compact('page_title')); ?>
... blah blah ...
<?php $this->load->view($footer); ?>