简体   繁体   中英

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

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

then "body.php" is like this

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

lastly "scripts.php"

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

then in my "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

Filename: 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

header.php

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

body.php

    lots of stuff..

scripts.php or 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. It could be something like:

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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