[英]Load View in View CodeIgniter
I want to load a view
which come from logical statement (in controller
) IN a view
. 我想在
view
中加载来自逻辑语句(在controller
)的view
。 Simply like this 就像这样
Controller 调节器
public function index(){
$this->lib();
$this->view('main');
}
public function lib(){
if(TRUE){
// $this->view('something1') in $this->view('main')
}
else{
// $this->view('something2') in $this->view('main')
}
}
** View ** **查看**
<html>
<body>
<!-- view from $this->view('main') -->
<!-- want to show view from lib() -->
</body>
</html>
How to show $this->view('something')
get from lib()
in $this->view('main')
? 如何显示
$this->view('something')
从$this->view('main')
lib()
获取? Any idea ? 任何想法 ?
The only way that I've found works is this: 我发现有效的唯一方法是:
Controller: 控制器:
public function index() {
$data['internal_view'] = $this->load->view('stmt', [], true);
$this->load->view('main', $data);
}
"main" View: “主”视图:
<html>
<body>
<?php echo $internal_view; ?>
</body>
</html>
Note: the 3rd param in view
allows for the view to be returned as a string rather than automatically outputted to the browser. 注意:
view
的第三个参数允许视图以字符串形式返回,而不是自动输出到浏览器。 Because of this functionality, you can assign a view as a return string from another function and use it to generate internal_view
or whatever you decide to call it. 由于此功能,您可以将视图分配为另一个函数的返回字符串,并使用它生成
internal_view
或您决定调用的任何内容。
You could preserve the main view and also keep the lib conditional view and pass it to the main controller. 您可以保留主视图,还可以保留lib条件视图,并将其传递给主控制器。
Controller : 控制器:
public function index(){
$data['lib_view'] = $this->lib();
$this->view('main', $data);
}
public function lib(){
if(TRUE){
return $this->load->view('something1', true);
}
else{
return $this->load->view('something2', true);
}
}
'main' View : “主”视图:
<html>
<body>
<!-- view from $this->view('main') -->
<?php echo $lib_view ?>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.