2012-04-14 44 views

回答

3

製作一個佈局視圖。有些事情是這樣的:

<html> 
    <head> 
    <title>My awesome site</title> 
    </head> 
    <body> 
    <?= $this->load->view('shared/header') ?> 
    <?= $this->load->view($partial) ?> 
    <?= $this->load->view('shared/footer') ?> 
    </body> 
</html> 

然後在你的控制器做到這一點:

$this->data['partial'] = 'pages/index'; 
$this->load->view('layout/layout', $this->data); 

如果你有一個MY_Controller文件,你可以創建有一個方法:在控制器

function load_view($partial) 
{ 
    $this->data['partial'] = $partial; 
    $this->load->view('layout/layout', $this->data); 
} 

那麼這繼承自MY_Controller,您可以使用:

$this->load_view('pages/index'); // pages/index will be loaded in the layout 
0

您可以隨時使用其他視圖加載視圖並實現所需內容。

其他更好的辦法是使用支持模板繼承像

CI中使用這些模板引擎是相當容易的,因爲有許多CI的庫的實現。

2

我知道其中的一種方法是爲您的網站/項目創建頁眉和頁腳視圖。

application/views/template/header.php 
application/views/template/footer.php 

然後你有你的其他頁面如何安排你想要的。

application/views/users/login.php 

在你的控制,你會做這樣的事情

<?php 
    class Blah extends CI_Controller 
    { 
     public function __construct() 
     { 
      parent::__construct(); 
     } 

     public function index() 
     { 
      $this->load->view('template/header'); 
      $this->load->view('users/login'); 
      $this->load->view('template/footer'); 
     } 
    } 

還有一個有用的小模板庫我偶然發現,使這個就更簡單了。但我覺得它不像輸入另一種方式那麼靈活。

http://maestric.com/doc/php/codeigniter_template

編輯:: 我應該給予一些示例代碼爲模板,在這裏你去

的header.php

<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="utf-8" /> 
     <title>Lovely Title</title> 
    </head> 
    <body> 
     <h1>Check This Out!!1!</h1> 
     <div class="container"> 

footer.php

 </div> 
    </body> 
</html> 
+0

,這裏沒有人喜歡發佈整個代碼塊我不認爲在控制器中加載頁眉和頁腳是一個好主意。你會一遍又一遍地重複着自己。最好創建一個佈局視圖文件並在其中加載頁眉/頁腳。那麼你只需要做一次。 – Mischa 2012-04-14 13:40:33

+0

是的,我提供的鏈接和你的建議完全一樣。皮膚貓太多的方法:) – Dale 2012-04-15 09:20:17

相關問題