我要开始一个大型的CodeIgniter项目,并想尝试创建一些可重用的“迷你”视图,用于显示在不同页面/控制器上的数据循环等内容片段。
从主控制器的视图中调用视图是否更好?如果是,怎么做?还是应该从控制器中调用“迷你视图”,因此将视图的代码传递给主视图?
从主控制器的视图中调用视图是否更好?如果是,怎么做?还是应该从控制器中调用“迷你视图”,因此将视图的代码传递给主视图?
在其他视图中嵌套的视图称为嵌套视图。在CodeIgniter中有两种包含嵌套视图的方式:
预先加载视图并传递给其他视图。首先在控制器中添加以下内容:
<?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?>
放在视图中您想要菜单出现的位置。
<?php
$this->load->view('home');
?>
然后将此代码放入/application/views/home.php
视图中:
<?php $this->view('menu'); ?>
<p>Other home content...</p>
TRUE
参数)时,你正在将所有的视图内容转换成一个字符串。这通常能够正常工作,但一旦你有重量级数据,或者当你传递数组或JSON时,它就会出问题。 - CodeGodie$menu
…如果您查看“home”,您不知道$menu
来自哪里… - dsdsdsdsdtemplate.php
$this->load->view('header',$title);
$this->load->view('sidebar',$sidebar_content);
$this->load->view('main_content',$main_content);
$this->load->view('footer');
$data['title'] = 'Home Page';
$data['sidebar_content']='pages/standard_sidebar';
$data['main_content'] ='pages/my_home_page';
$this->load->view('template',$data);
是的,我可以在每个主视图页面中包含头部、侧边栏和页脚,但这最终会导致大量重复的代码。如果例如,我想让所有页面都有一些新的东西,一些其他的小片段,使用模板,我只需将片段添加到适当的模板中即可完成。而采用另一种方法,我需要找到每个页面并在那里添加片段视图,这相当于在页面中使用CSS,浪费且不易维护。
方法1
我在视图中使用此方法将包含视图插入到所需的位置。
$this->load->view('include/include_view');
方法2
或者在控制器中,您可以像这样加载多个视图:
$this->load->view('header_view');
$this->load->view('list_view');
$this->load->view('footer_view');
没有哪种方法比另一种更好,这取决于您是否需要传递一些数据(在这种情况下使用method2),或者您是否想在主视图的特定部分中包含视图(在这种情况下最好使用method1)。
METHOD 3
通过主视图将数据传递给您的包含视图
进入您的控制器:
$data['title'] = "Title";
$this->load->view('main_view',$data);
$data2['title'] = $title;
$this->load->view('include/include_view',$data2);
如果您想将整个数据传递给include视图,可以按照以下方式进行操作: 在您的控制器中:
$data['nestedView']['title'] = 'title';
$this->load->view('includes/included_view', $nestedView);
$data['middle'] = 'includeFolder/include_template_view'; //the view you want to include
$this->load->view('main_template_view',$data); //load your main view
$this->load->view($middle);
我认为为了更有效地解决这个问题,我已经这样做:
您可以创建一个新的帮助程序(在应用程序/ helpers中),并命名为(例如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");
}
class Hello extends CI_Controller {
public function index(){
$this->load->helper('common');
$this->load->view('index');
}
}
<?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);
}
?>
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');
?>