简体   繁体   English

Codeigniter处理控制器调用视图的最佳方法

[英]Codeigniter best way to handle controller calling views

i just want to know if this is a good way in adding fragmented views. 我只是想知道这是否是添加碎片视图的好方法。

lets say "header.php" is like this 可以说“ header.php”是这样的

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

then "body.php" is like this 那么“ body.php”就是这样

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

lastly "scripts.php" 最后是“ scripts.php”

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

then in my "MyController.php" 然后在我的“ MyController.php”中

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

The best way I find is create a default view. 我发现最好的方法是创建一个默认视图。

views > default_view.php

views > includes > header_view.php

views > includes > footer_view.php

views > information > contact_view.php

On that view 在那个观点上

<?php

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

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

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

?>

Then on the controller to load view this way you do not have to load the header and footer views all the time. 然后,在控制器上以这种方式加载视图时,您不必一直加载页眉和页脚视图。

Following the Codeigniter StyleGuide 遵循Codeigniter StyleGuide

Filename: Example.php 文件名: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);

}

}

There are too many redundant tag like <html> , <body> try separate it 有太多多余的标签,例如<html><body>尝试将其分开

header.php header.php

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

body.php body.php

    lots of stuff..

scripts.php or footer.php scripts.php或footer.php

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

It is not good way. 这不是好方法。 Except in some cases (using frames for example), document should have just one declaration and one pair of open/closed html tag. 除非在某些情况下(例如使用框架),否则文档应仅具有一个声明和一对打开/关闭的html标签。 It could be something like: 可能是这样的:

header.php header.php

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

some_page.php some_page.php

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

footer.php footer.php

        <div class="footer">
        //&copy;2016
        </div>
    </body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM