在CI中加载页眉和页脚视图 [英] Load header and footer view in CI

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

问题描述

有没有任何方法加载视图'header'/'footer'而不调用 $ this-> load-> view('header') $ this-> load-> view('footer')在每个控制器?

Is there any way to load view 'header'/'footer' without calling $this->load->view('header') or $this->load->view('footer') in every controller? Maybe a template that can be use in every view?

推荐答案

这里有几个简单的方法让你开始:

Here are a couple simple approaches to get you started:

class Template {

    function load($view)
    {
        $CI = &get_instance();
        $CI->load->view('header');
        $CI->load->view($view);
        $CI->load->view('footer');
    }

}

>

Usage in controller:

$this->template->load('my_view');



使用主视图文件:



Use a master view file:

<!-- views/master.php -->
<html>
  <header>Your header</header>
  <?php $this->load->view($view, $data); ?>
  <footer>Your footer</footer>
</html>

在控制器中:

$this->load->view('master', array(
    'view' => 'my-view-file',
    'data' => $some_data
));






我更喜欢 类方法,因为它很容易添加方法附加模板区域,加载javascript文件,以及任何其他您需要的。我也喜欢根据被调用的方法自动选择视图文件。像这样:


I prefer the Template class approach, as it's easy to add methods to append templates areas, load javascript files, and whatever else you need. I also prefer to automatically select the view file based on the method being called. Something like this:

if ( ! isset($view_file)) {
    $view_file = $CI->router->fetch_class().'/'.$CI->router->fetch_method();
}

这将加载 views / users / index.php 如果控制器 Users 且方法是 index

This would load views/users/index.php if the controller is Users and the method is index.

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

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