Best Practices: What's the Best Way for Constructing Headers and Footers?

前端 未结 4 865
花落未央
花落未央 2021-01-30 07:46

What\'s the best way for constructing headers, and footers? Should you call it all from the controller, or include from the view file? I\'m using CodeIgniter, and I\'m wanting

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-30 08:14

    It's bad practice to call views inside of other views. This could be a form of controller view mixing. The view function in CI allows you to pass a third parameter that causes it to return that views output as a string. You can use this to create a compound view.

    For example:

    class Page extends Controller {
       function index() {
          $data['page_title'] = 'Your title';
    
          $this->load->view('default_layout', array(
             'header'  => $this->load->view('header' , array(), true), 
             'menu'    => $this->load->view('menu'   , array(), true), 
             'content' => $this->load->view('content', $data  , true), 
             'footer'  => $this->load->view('footer' , array(), true), 
          ));
       }
    }
    

    default_layout.php

    
    

    You may want to combine your header and footer to make a template like this.

    class Page extends Controller {
       function index() {
          $data['page_title'] = 'Your title';
    
          $this->load->view('default_template', array(
             'menu'    => $this->load->view('menu'   , array(), true), 
             'content' => $this->load->view('content', $data  , true), 
          ));
       }
    }
    

    default_template.php

    Some Header HTML // this is your header html
    
    some footer HTML  // this is your footer html
    

提交回复
热议问题