在 CodeIgniter 中的视图中包含视图的最佳方法 [英] Best method of including views within views in CodeIgniter

查看:34
本文介绍了在 CodeIgniter 中的视图中包含视图的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在启动一个大型 codeigniter 项目,并想尝试为内容片段创建一些可重复使用的迷你"视图,例如可能显示在不同页面/控制器上的数据循环.

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.

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

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?

推荐答案

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

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

提前加载视图并传递给另一个视图.首先把它放在控制器中:

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);
?>

然后将 <?=$menu?> 放在您希望菜单出现的位置.

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

首先将其放入控制器中:

First put this in the controller:

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

然后把它放在/application/views/home.php视图中:

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

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

关于最佳方法,我更喜欢第一种方法而不是第二种方法,因为使用第一种方法我不必混淆代码,它不像 include php.虽然间接两者相同,但第一种方法更清晰&比第二个干净!

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!

这篇关于在 CodeIgniter 中的视图中包含视图的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆