简体   繁体   English

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

[英]Best method of including views within views in 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.我正在启动一个大型 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?或者我应该从控制器调用“迷你视图”,从而将视图的代码传递给主视图?

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 1. 在控制器内加载嵌套视图

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.然后将<?=$menu?>放在您希望菜单出现的位置。

2. Load a view "from within" a view 2.从视图“内部”加载视图

First put this in the controller:首先把它放在控制器中:

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

Then put this in the /application/views/home.php view:然后把它放在/application/views/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.关于最佳方法,我更喜欢第一种方法而不是第二种方法,因为使用第一种方法我不必混淆代码,它不像包含php。 Although indirectly both are same, the 1st method is clearer & cleaner than 2nd one!虽然间接两者相同,但第一种方法比第二种方法更清晰更干净!

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.老实说,我更喜欢通过使用模板视图然后从控制器加载必要的数据来做到这一点,这意味着重复代码少得多,并且比从视图加载视图更好地遵循 DRY 概念。 Especially for things like headers, footers and menus.特别是对于页眉、页脚和菜单等内容。

So my template view would look something like this:所以我的模板视图看起来像这样:

template.php模板.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.走另一条路,我找到每个页面并在那里添加代码段视图,在我看来,这相当于在页面中使用 CSS,既浪费又无法最终维护。

METHOD 1方法一

I use this method into my view to insert the include view where I want我使用这个方法到我的视图中,在我想要的地方插入包含视图

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


METHOD 2方法二

or in the controller you can load more than a view like this:或者在控制器中,您可以加载更多这样的视图:

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

No one method is better than the other, it depends if you have to pass some data (in this case use method2) or if you want to include a view in a specific part of your main view (in this case is better to use method1)没有一种方法比另一种方法更好,这取决于您是否必须传递一些数据(在这种情况下使用方法 2)或者您是否想在主视图的特定部分中包含视图(在这种情况下最好使用方法 1 )


METHOD 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 way: in your controller:如果您想将整个数据传递给包含视图,您可以这样做:在您的控制器中:

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

in your view在你看来

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

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

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).您创建一个新的助手(在 application/helpers 中),名称为(es.common_helpers.php,下划线很重要)。 In this file, you put all the functions for example build pieces of html in common.在此文件中,您将所有功能(例如构建 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');   
   }

}

In the controller在控制器中

controller控制器

<?php
    public function view($page = NULL)
        {
          if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
        {
                 $data['title'] = ucfirst($page); // Capitalize the first letter
                // Whoops, we don't have a page for that
                show_404();

        }

        $data= array(''); 
        $data['title'] = ucfirst($page); // Capitalize the first letter 
         $data['page_layout']='pages/'.$page;    
         $this->load->view('page_layout', $data);
        }
?>

In the Views folder create a page called page_layout.php在 Views 文件夹中创建一个名为 page_layout.php 的页面

page_layout.php

//This is where you set the layout to call any view through a variable called $page_layout declared in the controller//

 <?php
     $this->load->view('header');
     $this->view($page_layout);
     $this->load->view('footer');
  ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM