CodeIgniter / PHP - 从视图中调用视图 [英] CodeIgniter/PHP - Calling a view from within a view
问题描述
基本上对于我的webapp我试图组织它好一点。因为它目前,每次我想加载一个页面,我必须这样从我的控制器,如:
Basically for my webapp I'm trying to organise it a bit better. As it at the moment, every time I want to load a page, I have to do it from my controller like so:
$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');
因为你可以说它不是非常高效。
As you can tell it's not really very efficient.
所以我想我会创建一个主视图 - 这叫做template.php。这是模板视图的内容:
So I thought I'd create one 'master' view - It's called template.php. This is the contents of the template 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');
?>
然后我想我可以从这样的控制器调用它:
And then I thought I'd be able to call it from a controller like this:
$data['view'] = 'homecontent';
$this->load->view('template',$data);
不幸的是,我根本无法使这项工作。有谁有任何方式围绕这个或修复我可以放到位?我试着在template.php的$视图中放置和,但是没有什么区别。通常的错误是未定义的变量:数据或无法加载视图:$ view.php等。
Unfortunately I simply cannot make this work. Does anyone have any ways around this or fixes I can put into place? I've tried putting ""s and ''s around $view in template.php but that makes no difference. The usual error is "Undefined variable: data" or "Cannot load view: $view.php" etc.
谢谢大家!
杰克
推荐答案
我相信你有:
$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']。
This is because when the array is passed from the controller, the variable on the view can be accessed simply by $view rather than $data['view'].
这篇关于CodeIgniter / PHP - 从视图中调用视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!