CodeIgniter中的页眉和页脚

goo*_*ing 68 php codeigniter

我真的不喜欢在每个控制器中写作:

    $this->load->view('templates/header');
    $this->load->view('body');
    $this->load->view('templates/footer');
Run Code Online (Sandbox Code Playgroud)

是否可以这样做,页眉和页脚将自动包含在内,如果我们需要更改它,我们也可以这样做?你怎么处理那件事呢?或者你认为这不是问题?谢谢.

lan*_*ons 102

这是我做的:

<?php

/**
 * /application/core/MY_Loader.php
 *
 */
class MY_Loader extends CI_Loader {
    public function template($template_name, $vars = array(), $return = FALSE)
    {
        $content  = $this->view('templates/header', $vars, $return);
        $content .= $this->view($template_name, $vars, $return);
        $content .= $this->view('templates/footer', $vars, $return);

        if ($return)
        {
            return $content;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

对于CI 3.x:

class MY_Loader extends CI_Loader {
    public function template($template_name, $vars = array(), $return = FALSE)
    {
        if($return):
        $content  = $this->view('templates/header', $vars, $return);
        $content .= $this->view($template_name, $vars, $return);
        $content .= $this->view('templates/footer', $vars, $return);

        return $content;
    else:
        $this->view('templates/header', $vars);
        $this->view($template_name, $vars);
        $this->view('templates/footer', $vars);
    endif;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,在您的控制器中,您需要做的就是:

<?php
$this->load->template('body');
Run Code Online (Sandbox Code Playgroud)

  • 忽略我刚才说的话.手册中的答案:"如果您将参数设置为true(布尔值),它将返回数据.默认行为为false,将其发送到您的浏览器.如果您想要返回数据,请记住将其分配给变量:" ."if($ return)"只是一个备份. (3认同)
  • 我向应用程序/核心添加了一个文件LS_Loader.php,并给出了你给出的回复,但是当我调用$ this-> load-> template('my_template',$ data)时,它会抛出一个错误`调用未定义的方法CI_Loader ::模板()`.还有其他步骤吗?或者这不适用于2.1.3? (2认同)
  • @mehulved"MY_"是可配置的,但在CI中默认为"MY_",并且对于所有自定义覆盖都是相同的.改变它可能会破坏其他插件 (2认同)

Jor*_*eno 42

是.

创建文件夹中调用template.phpviews文件.

内容template.php:

$this->load->view('templates/header');
$this->load->view($v);
$this->load->view('templates/footer');
Run Code Online (Sandbox Code Playgroud)

然后从你的控制器你可以做一些事情:

$d['v'] = 'body';
$this->load->view('template', $d);
Run Code Online (Sandbox Code Playgroud)

这实际上是我个人加载所有视图的非常简单的版本.如果你把这个想法发挥到极致,你可以制作一些有趣的模块化布局:

考虑一下,如果创建一个init.php包含单行的视图:

$this->load->view('html');
Run Code Online (Sandbox Code Playgroud)

现在创建html.php包含内容的视图:

<!DOCTYPE html>
<html lang="en">
    <? $this->load->view('head'); ?>
    <? $this->load->view('body'); ?>
</html>
Run Code Online (Sandbox Code Playgroud)

现在创建一个head.php包含内容的视图:

<head>
<title><?= $title;?></title>
<base href="<?= site_url();?>">
<link rel="shortcut icon" href='favicon.ico'>
<script type='text/javascript'>//Put global scripts here...</script>
<!-- ETC ETC... DO A BUNCH OF OTHER <HEAD> STUFF... -->
</head>
Run Code Online (Sandbox Code Playgroud)

body.php与内容视图:

<body>
    <div id="mainWrap">
        <? $this->load->view('header'); ?>
        <? //FINALLY LOAD THE VIEW!!! ?>
        <? $this->load->view($v); ?>
        <? $this->load->view('footer'); ?>
    </div>
</body>
Run Code Online (Sandbox Code Playgroud)

并酌情创建header.phpfooter.php查看.

现在,当您从控制器调用init时,所有繁重的工作都已完成,您的视图将被包含在内部<html><body>标签中,您的页眉和页脚将被加载.

$d['v'] = 'fooview'
$this->load->view('init', $d);
Run Code Online (Sandbox Code Playgroud)


And*_*lin 7

试试以下

文件夹结构

-application
 --controller
   ---dashboards.php
 --views
   ---layouts
      ----application.php
   ---dashboards
      ----index.php
Run Code Online (Sandbox Code Playgroud)

调节器

class Dashboards extends CI_Controller
{

   public function __construct()
   {
     parent::__construct();
     $data                = array();
     $data['js']          = 'dashboards.js'
     $data['css']         = 'dashbaord.css'
   }

   public function index()
   { 
     $data                = array();
     $data['yield']       = 'dashboards/index';

     $this->load->view('layouts/application', $data);
   }
}
Run Code Online (Sandbox Code Playgroud)

视图

<!DOCTYPE html>
<html>
   <head>
      <meta charset="UTF-8" />
      <title>Some Title</title>
      <link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/app.css" />
      <link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/<?php echo $css; ?>" />
   </head>
   <body>
     <header></header>
     <section id="container" role="main">
     <?php $this->load->view($yield); ?>
     </section>
     <footer></footer>
     <script src="<php echo base_url(); ?>assets/js/app.js"></script>
     <script src="<php echo base_url(); ?>assets/js/<?php echo $js; ?>"></script>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

当您需要在页眉或页脚中加载不同的js,css或其他任何内容时,请使用该__construct函数$this->load->vars

这种方式类似铁路