在CodeIgniter中包含视图的最佳方法。

时间:2022-10-06 23:59:58

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。虽然间接地两者都是一样的,但是第一种方法比第二种更清晰、更干净!

#2


14  

Honestly I prefer to do this by having template views then loading that with the necessary data from the controller, it means a lot less repeated code and follows the DRY concept better than loading views from views. Especially for things like headers, footers and menus.

老实说,我更喜欢使用模板视图来实现这一点,然后用来自控制器的必要数据来加载这些视图,这意味着重复代码要少得多,并且比从视图加载视图更好地遵循DRY概念。特别是对于页眉、页脚和菜单。

So my template view would look something like this:

我的模板视图是这样的

template.php

template.php

$this->load->view('header',$title);
$this->load->view('sidebar',$sidebar_content);
$this->load->view('main_content',$main_content);
$this->load->view('footer');

Then in my controller I pass the data required to the template like this:

然后,在我的控制器中,我将所需的数据传递给模板,如下所示:

$data['title'] = 'Home Page';
$data['sidebar_content']='pages/standard_sidebar';
$data['main_content'] ='pages/my_home_page'; 
$this->load->view('template',$data);

There are a number of benefits to doing it this way. First is I can have multiple templates, for example I have, in my case, two main ones, one for full page views without a sidebar and one for pages with a sidebar, I also call an if statement to decide which header to include, the regular one or the one with the admin menu in it.

这样做有很多好处。首先是我可以有多个模板,比如我,在我的例子中,两个主要的,一个完整的页面浏览量没有侧边栏和页边栏,我也叫一个if语句来决定哪个头包括定期与管理一个或一个菜单。

Yes I could include the header, sidebar and footer in every main view page, but that ends up in a ton of duplicate code. And what happens if for example I want all my pages to have something new, some other small snippet? Using templates I add the snippet to the appropriate template and it's done. Going the other route I find every page and add the snippet view there, it's the equivalent to having CSS in the page in my opinion, wasteful and not ultimately maintainable.

是的,我可以在每个主视图页面中包含页眉、侧边栏和页脚,但那将导致大量重复的代码。如果我想让我所有的页面都有一些新的东西,一些其他的小片段,会发生什么?使用模板,我将代码片段添加到适当的模板中,这样就完成了。按照另一种方式,我找到每一个页面并在其中添加snippet视图,在我看来,这相当于在页面中添加CSS,既浪费又不能维护。

#3


4  

METHOD 1

方法1

I use this method into my view to insert the include view where I want

我在视图中使用这个方法来插入我想要的包含视图。

$this->load->view('include/include_view');


METHOD 2

方法2

or in the controller you can load kore than a view like this:

或者在控制器中,你可以加载kore,而不是像这样的视图:

$this->load->view('header_view');
$this->load->view('list_view');
$this->load->view('footer_view');

Isn't a better method, depends if you have to pass some data (int his case use the method2) or if you want to include a view in a specific part of your main view (in this case is better to use the method 1)

这不是一个更好的方法,这取决于您是否需要传递一些数据(在他的案例中使用method2),或者如果您想在主视图的特定部分中包含一个视图(在本例中,最好使用方法1)


METHOD 3

方法3

Passing data to your include view by your main view

通过主视图将数据传递给包含视图

into your controller:

控制器:

$data['title'] = "Title";
$this->load->view('main_view',$data);

in your view

在你的观点

$data2['title'] = $title;
$this->load->view('include/include_view',$data2);

If you want to pass entire data to your include view you can do in this mode: in your controller:

如果您想将整个数据传递给包含视图,可以使用以下模式:在控制器中:

$data['nestedView']['title'] = 'title';

in your view

在你的观点

$this->load->view('includes/included_view', $nestedView);

#4


4  

This a simple way of including views within views.there is no need to load views in advance.just pass view path to other view.

这是包含视图内视图的简单方法。不需要预先加载视图。通过查看路径到其他视图。

In your controller use this:

在您的控制器中使用:

$data['middle'] = 'includeFolder/include_template_view';  //the view you want to include
$this->load->view('main_template_view',$data);  //load your main view

and in main_template_view you can include other views :

在main_template_view中,可以包含其他视图:

$this->load->view($middle);

#5


1  

In my opinion for solve in more efficient way this problem I have done so:

在我看来,为了更有效地解决这个问题,我这样做了:

You create a new helper (in application/helpers) with name (es. common_helpers.php, the underscore is important). In this file, you put all the functions for example build pieces of html in common.

您将创建一个具有名称的新助手(在应用程序/助手中)。common_helpers。php,下划线很重要)。在这个文件中,您将所有的函数(例如构建html片段)放在一起。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    function getHead(){
    require_once(APPPATH."views/common/head.php");
    }   

    function getScripts(){
    require_once(APPPATH."views/common/scripts.php");
    }

    function getFooter(){
    require_once(APPPATH."views/common/footer.php");
    }

In your controller you call only one view in respect of MVC and call the functions from your custom helper.

在您的控制器中,您只调用一个MVC视图,并从自定义助手调用函数。

class Hello extends CI_Controller {

   public function index(){
       $this->load->helper('common');
       $this->load->view('index');   
   }

}

#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。虽然间接地两者都是一样的,但是第一种方法比第二种更清晰、更干净!

#2


14  

Honestly I prefer to do this by having template views then loading that with the necessary data from the controller, it means a lot less repeated code and follows the DRY concept better than loading views from views. Especially for things like headers, footers and menus.

老实说,我更喜欢使用模板视图来实现这一点,然后用来自控制器的必要数据来加载这些视图,这意味着重复代码要少得多,并且比从视图加载视图更好地遵循DRY概念。特别是对于页眉、页脚和菜单。

So my template view would look something like this:

我的模板视图是这样的

template.php

template.php

$this->load->view('header',$title);
$this->load->view('sidebar',$sidebar_content);
$this->load->view('main_content',$main_content);
$this->load->view('footer');

Then in my controller I pass the data required to the template like this:

然后,在我的控制器中,我将所需的数据传递给模板,如下所示:

$data['title'] = 'Home Page';
$data['sidebar_content']='pages/standard_sidebar';
$data['main_content'] ='pages/my_home_page'; 
$this->load->view('template',$data);

There are a number of benefits to doing it this way. First is I can have multiple templates, for example I have, in my case, two main ones, one for full page views without a sidebar and one for pages with a sidebar, I also call an if statement to decide which header to include, the regular one or the one with the admin menu in it.

这样做有很多好处。首先是我可以有多个模板,比如我,在我的例子中,两个主要的,一个完整的页面浏览量没有侧边栏和页边栏,我也叫一个if语句来决定哪个头包括定期与管理一个或一个菜单。

Yes I could include the header, sidebar and footer in every main view page, but that ends up in a ton of duplicate code. And what happens if for example I want all my pages to have something new, some other small snippet? Using templates I add the snippet to the appropriate template and it's done. Going the other route I find every page and add the snippet view there, it's the equivalent to having CSS in the page in my opinion, wasteful and not ultimately maintainable.

是的,我可以在每个主视图页面中包含页眉、侧边栏和页脚,但那将导致大量重复的代码。如果我想让我所有的页面都有一些新的东西,一些其他的小片段,会发生什么?使用模板,我将代码片段添加到适当的模板中,这样就完成了。按照另一种方式,我找到每一个页面并在其中添加snippet视图,在我看来,这相当于在页面中添加CSS,既浪费又不能维护。

#3


4  

METHOD 1

方法1

I use this method into my view to insert the include view where I want

我在视图中使用这个方法来插入我想要的包含视图。

$this->load->view('include/include_view');


METHOD 2

方法2

or in the controller you can load kore than a view like this:

或者在控制器中,你可以加载kore,而不是像这样的视图:

$this->load->view('header_view');
$this->load->view('list_view');
$this->load->view('footer_view');

Isn't a better method, depends if you have to pass some data (int his case use the method2) or if you want to include a view in a specific part of your main view (in this case is better to use the method 1)

这不是一个更好的方法,这取决于您是否需要传递一些数据(在他的案例中使用method2),或者如果您想在主视图的特定部分中包含一个视图(在本例中,最好使用方法1)


METHOD 3

方法3

Passing data to your include view by your main view

通过主视图将数据传递给包含视图

into your controller:

控制器:

$data['title'] = "Title";
$this->load->view('main_view',$data);

in your view

在你的观点

$data2['title'] = $title;
$this->load->view('include/include_view',$data2);

If you want to pass entire data to your include view you can do in this mode: in your controller:

如果您想将整个数据传递给包含视图,可以使用以下模式:在控制器中:

$data['nestedView']['title'] = 'title';

in your view

在你的观点

$this->load->view('includes/included_view', $nestedView);

#4


4  

This a simple way of including views within views.there is no need to load views in advance.just pass view path to other view.

这是包含视图内视图的简单方法。不需要预先加载视图。通过查看路径到其他视图。

In your controller use this:

在您的控制器中使用:

$data['middle'] = 'includeFolder/include_template_view';  //the view you want to include
$this->load->view('main_template_view',$data);  //load your main view

and in main_template_view you can include other views :

在main_template_view中,可以包含其他视图:

$this->load->view($middle);

#5


1  

In my opinion for solve in more efficient way this problem I have done so:

在我看来,为了更有效地解决这个问题,我这样做了:

You create a new helper (in application/helpers) with name (es. common_helpers.php, the underscore is important). In this file, you put all the functions for example build pieces of html in common.

您将创建一个具有名称的新助手(在应用程序/助手中)。common_helpers。php,下划线很重要)。在这个文件中,您将所有的函数(例如构建html片段)放在一起。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    function getHead(){
    require_once(APPPATH."views/common/head.php");
    }   

    function getScripts(){
    require_once(APPPATH."views/common/scripts.php");
    }

    function getFooter(){
    require_once(APPPATH."views/common/footer.php");
    }

In your controller you call only one view in respect of MVC and call the functions from your custom helper.

在您的控制器中,您只调用一个MVC视图,并从自定义助手调用函数。

class Hello extends CI_Controller {

   public function index(){
       $this->load->helper('common');
       $this->load->view('index');   
   }

}