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

查看:36
本文介绍了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;
        }
    }
}

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

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

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

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

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

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