2012-06-19 96 views

回答

6

是的,您可以在控制器顶部的__construct函数中加载视图。就拿 看看PHP手册上Constructors

function __construct() 
{  
     parent::__construct(); 
     $this->load>-view('your_view'); 

} 

如果headerfooter将要为您的网站的视觉部分常量和必需的组件,但你可能要加载之间的不同内容部分的页眉和页脚,那么你可以创建一个函数来接受一个参数。

private function doViews($argument) 
{ 


     $this->load->view('header'); 

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

     $this->load->view('footer'); 


     return NULL; 
} 

您可能希望的doViews函数内可用的意见阵列,为了做到这一点的文件存在适当的验证。然后你只需调用每个方法的功能在你的控制器是这样的:

$this->doViews('main_content'); 
+0

哦,谢谢!出于某种原因,我没有意识到我可以在'__construct'中做更多的事情,只是加载一个模型;)但是如何处理页脚? –

+0

它只是一个功能,你可以用它做很多。 –

+0

我不知道为什么我没有意识到它。你能告诉我关于这个页脚吗? –

0

你应该尝试使用模板库这样的:https://github.com/philsturgeon/codeigniter-template

然后,所有你需要的把这个控制器(可

$this->template->build('create', $this->data); 
:可以在__construct或方法中)

$this->template->set_partial('header', 'layouts/header'); 
$this->template->set_partial('footer', 'layouts/footer'); 
$this->template->set_partial('sidebar', 'layouts/sidebar'); 

像你这样做视图然后发送数据0

0

,你可以创建你main_view ......作为一个已经有结构的母版页:

main_view.php

$this->load>-view('header'); 
<?php //get content here 
?> 
$this->load>-view('footer'); 

如果你想通过声明来改变在页眉或页脚的东西( )你可以添加内容:

function __construct() 
{  
     parent::__construct(); 

     $data['footer'] = ($a == 'foo') ? 'footer one' : 'footer two'; 

     $data_to_main = $this->load->view('template/footer', $data, TRUE); 

     $data_to_main = 'others'; 

     $this->load>-view('main_view', $data_to_main); 
}