2016-09-16 46 views
0

我只想知道這是否是添加碎片視圖的好方法。Codeigniter處理控制器調用視圖的最佳方式

可以說 「header.php文件」 就是這樣

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Bootstrap Admin Theme v3</title> 
    some links.. 
    </head> 
</html> 

然後 「body.php」 就是這樣

<!DOCTYPE html> 
<html> 
    <body> 
    lots of stuff.. 
    </body> 
</html> 

最後 「scripts.php」

<!DOCTYPE html> 
<html> 
    <body> 
    some scripts.. 
    </body> 
</html> 

那麼在我的「MyController.php」中

$this->load->view('header'); 
$this->load->view('body'); 
$this->load->view('scripts'); 
+0

我看到那麼就不能這樣做。我覺得這一個更清潔的方式,但它不好,那麼我想生病只是要按照你的建議。一個問題,如果在我的例子中看到,html標籤被重新聲明,會發生什麼? – n4mi

回答

1

,我覺得最好的辦法是創建一個默認視圖。

views > default_view.php 

views > includes > header_view.php 

views > includes > footer_view.php 

views > information > contact_view.php 

在控制器上查看

<?php 

$this->load->view('includes/header_view'); 

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

$this->load->view('includes/footer_view'); 

?> 

然後加載瀏覽這樣你就不必加載頁眉和頁腳視圖的所有時間。

繼笨StyleGuide

文件名:使用example.php

<?php 

class Example extends CI_Controller { 

public function index() { 

    // Add any other variables 

    $data['content_page'] = 'information/contact_view'; // This will be your content page example 

    $this->load->view('default_view', $data); 

} 

} 
+0

這很好。非常感謝 – n4mi

0

有太多多餘的標籤一樣<html><body>嘗試分開吧

的header.php

<!DOCTYPE html> 
<html> 
<head> 
    <title>Bootstrap Admin Theme v3</title> 
    some links.. 
</head> 
<body> 

body.php

lots of stuff.. 

scripts.php或footer.php

some scripts.. 
    </body> 
</html> 
0

這不是好的方法。除了在某些情況下(例如使用框架),文檔應該只有一個聲明和一對開放/關閉的html標籤。 這可能是這樣的:

的header.php

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Bootstrap Admin Theme v3</title> 
     some links.. 
    </head> 
    <body> 

some_page.php

 <div class="container"> 
      <div class="row"> 
      //some stuff and forms here 
      </div> 
     </div> 

footer.php

 <div class="footer"> 
     //&copy;2016 
     </div> 
    </body> 
</html> 
相關問題