2012-06-19 71 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); 
}