【问题标题】:Autoload a view on every page with codeigniter使用 codeigniter 在每个页面上自动加载视图
【发布时间】:2014-01-15 16:28:48
【问题描述】:

所以我的观点基本上分为三 (3) 个文件:

-- Header file 
$this->load->view('templates/header', $data);

-- Main Body file 
$this->load->view('login_view', $data);

-- Footer file 
$this->load->view('templates/footer', $data);

现在我刚刚开始构建,但我注意到在每个控制器上重新键入页眉和页脚以告诉它加载真的很烦人。有没有办法在每个请求上自动加载 headerfooter 视图?

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    这是我经常用我的 CI 项目做的简单例子。

    将主体部分作为$main 变量传递给控制器​​函数

        function test(){
        $data['main']='pages/about_us'; // this is the view file which you want to load
        $data['something']='some data';// your other data which you may need on view
        $this->load->view('index',$data);
    }
    

    现在在视图上加载$main 变量

    <html lang="en">
    <head>
    
    </head>
    <body>
    <div id="container">
        <?php $this->load->view('includes/header');?> 
    
        <div id="body">
       <?$this->load->view($main);?>
        </div>
    
        <?php $this->load->view('includes/footer');?>
    </div>
    
    </body>
    </html>
    

    通过这种方式,您始终可以使用index.php 来处理您的所有功能,只是$main 的值会有所不同。 快乐编码

    【讨论】:

      【解决方案2】:

      我很久以前找到了一篇文章,但我现在似乎找不到它,基本上是作者(我忘记了)覆盖了输出的显示。这种输出方法将访问您对给定controller/method 的视图,并尝试自动在您的视图目录中搜索。

      使用风险自负

      Application/core/MY_COntroller.php

      ------------------------------------------ --------------------------------

       class MY_Controller Extends CI_Controller
       {
            protected $layout_view = 'layouts/application'; // default
            protected $content_view =''; //data
            protected $view_data = array(); //data to be passed
      
        public function __construct()
        {
          parent::__construct();
        }
      
        public function _output($output)
        {
          if($this->content_view !== FALSE && empty($this->content_view)) $this->content_view = $this->router->class . '/' . $this->router->method;
      
          $yield = file_exists(APPPATH . 'views/' . $this->content_view . EXT) ? $this->load->view($this->content_view, $this->view_data, TRUE) : FALSE ;
      
          if($this->layout_view)
          {
              $html = $this->load->view($this->layout_view, array('yield' => $yield), TRUE);
              echo $html;
          }
        }
      
      }
      

      Application/views/layouts/layout.php

      ------------------------------------------ --------------------------------

      <html>
      <head>
      <title>master layout</title>
      </head>
      <body>
      <!-- this variable yeild is important-->
      <div><?=$yield;?></div>
      </body>
      </html>
      

      这是我用来创建我的模板的。基本上你需要一个目录结构如下。

      +观看次数
      |+布局
      ||-layout.php

      layout.php 将作为您的主模板

      如何使用?

      扩展控制器

      class User Extends MY_Controller
      {
         public function create_user()
         {
          //code here
         }
         public function delete_user()
        {
          //use a different master template
          $this->layout_view = 'second_master_layout';
        }
        public function show_user()
        {
          //pass the data to the view page
          $this->view_data['users'] = $users_from_db;
      
        }
      }
      

      只需在您的视图中创建目录并用控制器名称命名,即 user 然后在其中添加一个您命名为方法的文件,即 create_user

      所以现在你的目录结构将是

      +观看次数
      | +布局
      | |-layout.php
      | |-second_master_layout.php
      | +用户
      | |-create_user.php

      只需编辑代码即可为您提供动态的headerfooter

      【讨论】:

        【解决方案3】:

        您可以使用library

        创建一个名为template.php 的新library 文件并编写一个名为load_template 的函数。在那个函数中,使用上面的代码。

        public function load_template($view_file_name,$data_array=array()) {
        
        $ci = &get_instatnce();
        
        $ci->load->view("header");
        
        $ci->load->view($view_file_name,$data_array);
        
        $ci->> load->view("footer");
        
        }
        

        您必须将此库加载到配置文件夹中的autoload 文件中。所以你不想加载所有控制器。

        你可以打电话

        $this->template->load_template("index",$data_array);
        

        如果你想传递日期来查看文件,那么你可以通过 $data_array 发送

        【讨论】:

          【解决方案4】:

          ellislab 论坛上有一篇关于此主题的文章。请看一下。它可能会帮助你。

          http://ellislab.com/forums/viewthread/86991/

          替代方式:在关注体视图文件中加载页眉和页脚视图。这样,您可以对要包含的文件进行连击控制,以防您有多个用于不同目的的页眉和页脚文件。示例代码如下所示。

          <html lang="en">
          <head>
          
          </head>
          <body>
          <div id="container">
              <?php $this->load->view('header');?> 
          
              <div id="body">
              body
              </div>
          
              <?php $this->load->view('footer');?>
          </div>
          
          </body>
          </html>
          

          【讨论】:

            【解决方案5】:

            使用 MY_Controller:

            class MY_Controller extends CI_Controller {
                public $template_dir;
                public $header;
                public $footer;
            
                public function __construct() {
                    parent::__construct();
                    $template_dir = 'templates'; // your template directory
                    $header = 'header';
                    $footer = 'footer';
            
                    $this->template_dir = $template_dir;
                    $this->header = $header;
                    $this->footer = $footer;
                }
            
                function load_views ($main, $data = [], $include_temps = true) {
                    if ($include_temps = true) {
                         $this->load->view('$this->template_dir.'/'.$this->header);
                         $this->load->view($main);
                         $this->load->view('$this->template_dir.'/'.$this->footer);
                    } else {
                        $this->load->view($main);
                    }
                }
            }
            

            然后像这样加载它:$this-&gt;load_views('login_view', $data);

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2015-05-05
              • 1970-01-01
              • 2014-03-28
              • 1970-01-01
              • 2015-12-14
              • 1970-01-01
              • 2012-02-19
              相关资源
              最近更新 更多