【问题标题】:How to load page views like header, sidebar, footer in CodeIgniter?如何在 CodeIgniter 中加载页眉、侧边栏、页脚等页面视图?
【发布时间】:2014-03-20 11:08:15
【问题描述】:

我正在尝试在 CodeIgniter 中创建我的第二个 Web 应用程序。

在我之前的项目中,我为页眉、页脚和侧边栏创建了视图。

在每个页面控制器中,我都必须像这样加载这些视图:

class Home extends CI_Controller {

    public function index()
    {
        $this->load->view('header');
        $this->load->view('sidebar');
        $this->load->view('home'); // home
        $this->load->view('footer');
    }

    public function about()
    {
        $this->load->view('header');
        $this->load->view('sidebar');
        $this->load->view('about'); // about
        $this->load->view('footer');
    }

    public function contact()
    {
        $this->load->view('header');
        $this->load->view('sidebar');
        $this->load->view('contact'); // contact
        $this->load->view('footer');
    }
}

我不喜欢这样,我觉得我做错了。

有什么建议吗?

【问题讨论】:

    标签: php codeigniter templates


    【解决方案1】:

    预先,您可以通过这种方式构建您的视图:

    首先在您的视图文件夹中创建一个模板

    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <meta content="MajidGolshadi" name="author">
        <?php echo $html_head; ?>
        <title></title>
    </head>
    <body>
        <div id="content">
                <?php echo $content; ?>
        </div>
    
        <div id="footer">
            <?php echo $html_footer; ?>
        </div>
    </body>
    </html>
    

    第二 创建一个库来自动加载您的视图

    <?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Template
    {
        private $data;
        private $js_file;
        private $css_file;
        private $CI;
    
        public function __construct()
        {
            $this->CI =& get_instance();
            $this->CI->load->helper('url');
    
            // default CSS and JS that they must be load in any pages
    
            $this->addJS( base_url('assets/js/jquery-1.7.1.min.js') );
            $this->addCSS( base_url('assets/css/semantic.min.css') );
        }
    
        public function show( $folder, $page, $data=null, $menu=true )
        {
            if ( ! file_exists('application/views/'.$folder.'/'.$page.'.php' ) )
            {
                show_404();
            }
            else
            {
                $this->data['page_var'] = $data;
                $this->load_JS_and_css();
                $this->init_menu();
    
                if ($menu)
                    $this->data['content'] = $this->CI->load->view('template/menu.php', $this->data, true);
                else
                    $this->data['content'] = '';
    
                $this->data['content'] .= $this->CI->load->view($folder.'/'.$page.'.php', $this->data, true);
                $this->CI->load->view('template.php', $this->data);
            }
        }
    
        public function addJS( $name )
        {
            $js = new stdClass();
            $js->file = $name;
            $this->js_file[] = $js;
        }
    
        public function addCSS( $name )
        {
            $css = new stdClass();
            $css->file = $name;
            $this->css_file[] = $css;
        }
    
        private function load_JS_and_css()
        {
            $this->data['html_head'] = '';
    
            if ( $this->css_file )
            {
                foreach( $this->css_file as $css )
                {
                    $this->data['html_head'] .= "<link rel='stylesheet' type='text/css' href=".$css->file.">". "\n";
                }
            }
    
            if ( $this->js_file )
            {
                foreach( $this->js_file as $js )
                {
                    $this->data['html_head'] .= "<script type='text/javascript' src=".$js->file."></script>". "\n";
                }
            }
        }
    
        private function init_menu()
        {        
          // your code to init menus
          // it's a sample code you can init some other part of your page
        }
    }
    

    第三 在每个控制器构造函数中加载您的库,然后使用此库自动加载您的视图

    要在你的视图中加载额外的 js,你可以通过这种方式使用addJS 方法:

    $this->template->addJS("your_js_address");
    

    对于CSS:

    $this->template->addCSS("your_css_address");
    

    并使用show 方法调用您的视图文件来显示您的页面内容

    $this->template->show("your_view_folder", "view_file_name", $data);
    

    希望这些代码对你有帮助

    【讨论】:

    • 我认为您应该将代码精简到最基本的部分,但是是的,这是最好的解决方案。我曾经在 MY_Controller 中尝试过类似的解决方案,但这样更好。
    【解决方案2】:

    create template.php in view 把这段代码放进去

     <?php  
        $this->load->view('header');
        $this->load->view($middle);
        $this->load->view('footer');    
    ?>
    

    在控制器中使用此代码

    public function index()
        {
            $data['middle'] = 'home';
        $this->load->view('template',$data);
        }
    

    【讨论】:

      【解决方案3】:

      您可以在您使用的主模板文件中加载页眉、侧边栏、页脚。

      // contact.html
      <?php $this->load->view('header');?>
      <p>Yak yak yak</p>
      <?php $this->load->view('footer');?> 
      

      或者使用像this one这样的模板库

      【讨论】:

      • 那比我的还差,不太清楚。在我的代码中,我将所有内容都放在一个文件中,但复制了几个函数。我不喜欢将其复制到几个单独的文件中。
      【解决方案4】:

      您可以像这样创建一个库(或助手):

      <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
      
      class View_lib{
      
         function __construct(){
          $this->CI =& get_instance();
         }
      
      
         function load_view($current_page){
             $parts = array('header', 'sidebar', $current_page, 'footer');
             foreach($parts as $part){
             $this->CI->load->view($part);
          }
         }    
      }
      

      控制器:

      public function index(){
          $this->load->library('view_lib'); // probably better autoload this
          $this->view_lib->load_view('about');
      }
      

      有点准系统,但你明白了......

      【讨论】:

      • 有趣,但我可能需要以某种方式实现参数化视图处理。
      • 你可以很容易地将$data添加到load_view函数中作为第二个参数。然后在你的foreach中检查$part==$current_page,如果是$this-&gt;CI-&gt;load-&gt;view($part, $data);。在控制器中,您只需添加$data作为第二个参数。
      【解决方案5】:

      最好的方法是在构造函数中添加这些文件

      function __construct() { 
           parent::__construct(); 
              $this->load->helper('url'); 
              $this->load->database();
              $this->load->view('prots/header');
              $this->load->view('prots/top');
              $this->load->view('prots/navigation');
        } 
      

      【讨论】:

        【解决方案6】:
        Place all of them in a file like 
        main.php
        
        And load it on top of view rather than always sending from controller.
        
        main.php contain 
        <php 
        $this->load->view('header');
        $this->load->view('sidebar');
        $this->load->view('home');
        $this->load->view('footer');
        ?>
        
        and in view just place
        
        <php 
        $this->load->view('main');
        ?>
        
        at top of file
        

        【讨论】:

          猜你喜欢
          • 2010-12-14
          • 2017-09-24
          • 1970-01-01
          • 1970-01-01
          • 2012-01-25
          • 2019-12-09
          • 2014-03-08
          • 2023-03-16
          • 1970-01-01
          相关资源
          最近更新 更多