CodeIgniter中的页眉和页脚 [英] Header and footer in CodeIgniter

查看:156
本文介绍了CodeIgniter中的页眉和页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I really don't enjoy writing in every controller:

    $this->load->view('templates/header');
    $this->load->view('body');
    $this->load->view('templates/footer');

这是可能做到的,页眉和页脚会自动包含,如果我们需要更改,我们也可以这样做?你如何处理呢?或者你的意见不是一个问题?

Is it possible to do, that header and footer would be included automatically and if we need to change it, we could also do that? How do you deal with that? Or it's not a problem in your opinion? Thanks.

推荐答案

这是我所做的:

<?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;
        }
    }
}

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;
    }
}

然后,在您的控制器中,做:

Then, in your controller, this is all you have to do:

<?php
$this->load->template('body');

这篇关于CodeIgniter中的页眉和页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆