I'm starting a large codeigniter project and would like to try to create some reusable 'mini' views for snippets of content like loops of data which may be displayed on different pages/controllers.

我正在启动一个大型的codeigniter项目,我想尝试创建一些可重用的“迷你”视图,用于内容的片段,比如可以在不同的页面/控制器上显示的数据循环。

Is it better to call the views from within the main controller's view? If so, how? Or should I call the 'mini view' from the controller and thus pass the view's code to the main view?

从主控制器的视图中调用视图是否更好?如果是这样,如何?或者我应该从控制器调用“mini view”,从而将视图的代码传递给主视图?

5 个解决方案

#1


81

Views within other views are called Nested views. There are two ways of including nested views in CodeIgniter:

其他视图中的视图称为嵌套视图。在CodeIgniter中包含嵌套视图有两种方法:

1. Load a nested view inside the controller

Load the view in advance and pass to the other view. First put this in the controller:

提前加载视图并传递到另一个视图。首先把这个放在控制器中:

<?php
// the "TRUE" argument tells it to return the content, rather than display it immediately
$data['menu'] = $this->load->view('menu', NULL, TRUE);
$this->load->view ('home', $data);
?>

Then put <?=$menu?> in your view at the point you want the menu to appear.

然后把< ? = $菜单吗?在您的视图中,您希望菜单出现的位置出现>。

2. Load a view "from within" a view

First put this in the controller:

首先把这个放在控制器中:

<?php
  $this->load->view('home');
?>

Then put this in the /application/views/home.php view:

然后把这个放到/application/view /home。php视图:

<?php $this->view('menu'); ?>

<p>Other home content...</p>

About best method, I prefer the 1st method over 2nd one, because by using 1st method I don't have to mix up code, it is not like include php. Although indirectly both are same, the 1st method is clearer & cleaner than 2nd one!

关于最佳方法,我更喜欢第一种方法而不是第二种方法,因为使用第一种方法我不需要混淆代码,它不像包含php。虽然间接地两者都是一样的,但是第一种方法比第二种更清晰、更干净!

更多相关文章

  1. MySQL—嵌套select使用该表时如何从表中删除?
  2. 如何使用mysqldump在帐户之间复制视图?
  3. MySQL数据库8(二十)视图
  4. 如何知道php脚本中的名称是指mysql表还是mysql视图
  5. MySQL视图-(视图创建,修改,删除,查看,更新数据)
  6. ng- repeat显示的行等于no属性,甚至不显示html视图上的数据
  7. 桌面视图中的SlikNav多级菜单?
  8. 在单选按钮上选中/取消选中,加载/隐藏部分视图
  9. 如何在当前视图中始终保持水平底部滚动条

随机推荐

  1. 如何搭建go-micro开发环境
  2. 关于golang当中对select的理解
  3. Golang中使用JSON时区分空字段和未设置字
  4. Golang中Bit数组如何实现(代码示例)
  5. 详解Golang的反射(实例)
  6. Go语言实现之基于websocket浏览器通知功
  7. 关于用Go语言编程的利与弊
  8. 如何使用go优雅地撰写单元测试
  9. golang用什么开发工具?
  10. 详解 Go 语言中的方法