2016-09-16 74 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> 
相关问题