如何在Codeigniter中使用Header

时间:2017-07-05 02:01:01

标签: php codeigniter templates codeigniter-3

在上面的示例中,我们使用“动态添加的数据”,您将在下面看到。

控制器

public function index()
{

    $data['main_menus'] = '';
    if(count($this->menu_model->findActive()) > 0){
        $data['main_menus'] = $this->link_class->bootstrap_menu($this->menu_model->findActive());
    }

    $data['header'] = $this->load->view('common/header',$data);

    $data['posts'] = $this->home_model->find_active(3);
    $data['content'] = $this->load->view('common/content',$data);

    $this->load->view('layout',$data);


}

查看

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="es">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="DBlog : Material Style Personal Blog Theme from Wowbootstrap">
    <meta name="author" content="wowbootstrap.com">
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link href="http://fonts.googleapis.com/css?family=Open+Sans:400,300,700|Merriweather:400,400italic,700italic" rel="stylesheet" type="text/css">
    <link href="http://fonts.googleapis.com/css?family=Roboto:400,100,100italic,500,400italic,700,700italic,900,900italic,300,300italic,500italic" rel="stylesheet" type="text/css">
    <title><?php echo $page_title?> - CI Blog</title>
  </head>
  <body>
    <div id="wrapper">
      <?php echo $header;?>
      <section class="main">
        <div class="container">
          <div class="row">
            <div class="col-md-8">
            <?php //echo $content;?>
            </div>
            <div class="col-md-4">
            <?php //echo $right_sidebar;?>
            </div>
          </div>
        </div>
      </section>
      <?php //echo $footer;?>

    </div>


    <script>
      $.material.init();
    </script>

    <?php if(!empty($home_page)):?>
    <script type="text/javascript">

      $(window).scroll(function(){
        if ($(this).scrollTop() > 1){
          $('.top-navbar').removeClass('navbar-transparent');
        }else{
          $('.top-navbar').addClass('navbar-transparent');
        }
      });
    </script>
    <?php endif;?>

    <script type="text/javascript">
      $(document).ready(function () {
          $('[data-toggle="tooltip"]').tooltip();
      });
    </script>

</body>
</html>

enter image description here

我使用codeigniter开发php网站。现在我有了问题。 在控制器中,$this->load->view('index', $data); 我希望$data也可以分享header.php中包含的index.php

怎么办???

2 个答案:

答案 0 :(得分:3)

如果我在codeigniter上做模板,我确实喜欢这个

在Layout.php中

<!--Header -->
<?php  $this->load->view('path/to/header'); ?>

<!--Content -->
<?php  $this->load->view($content); ?>

<!--Footer-->
<?php  $this->load->view('path/to/footer'); ?>

在控制器中

$data['content'] = 'path/to/content/page'; # or any dynamic data with $this->model...

$this->load->view('template',$data);

答案 1 :(得分:0)

在你的控制器中:加载这样的视图文件,

public function index()
{

$data['main_menus'] = '';
if(count($this->menu_model->findActive()) > 0){
    $data['main_menus'] = $this->link_class->bootstrap_menu($this->menu_model->findActive());
}

$data['posts'] = $this->home_model->find_active(3);

// To load view files
$this->load->view('common/header',$data);    // header view file
$this->load->view('common/content',$data);    // body content view file
$this->load->view('layout',$data);            // footer view file

}