CI学习 – header和footer
方法一、在每个view文件的开头和结尾分别加上下面两句:
[php]
<?php $this->load->view(‘header’)?>
…
<?php $this->load->view(‘footer’)?>
[/php]
这也是最简单和容易理解的方法。
方法二、写一个模板类template.php,在里面实现这种逻辑,再提供一个showView()方法。头尾有各自的模型。
以下供参加:
[php]
<?php if (!defined(‘BASEPATH’))
exit(‘No direct script access allowed’);
class Template
{
private $mCI;
private $mHeaderView=’header.php’;//头部文件
private $mFooterView=’footer.php’;//尾部文件
private $mTemplateView=’template.php’;//模板框架
public function __construct()
{
$this->mCI = &get_instance();
}
public function showView($rContent_data)
{
//$rContent_data 在控制器中实现内容逻辑与视图
$data=array(
$header_data=$this->getHeader(),
$footer_data=$this->getFooter(),
$content_data=$rContent_data
);
$this->mCI->load->view($this->mTemplateView,$data);
}
private function getHeader()
{
$h=new HeaderModel();//实现头部逻辑,
$data=$h->getData();
return $this->mCI->load->view($this->mHeaderView,$data,true);
}
private function getFooter()
{
$f=new FooterModel();//实现尾部逻辑,
$data=$f->getData();
return $this->mCI->load->view($this->mFooterView,$data,true);
}
}
?>
[/php]
这种方式比较适用于头尾变化比较多的情况.
方式三:考虑到ajax请求的情况,使用hook,步骤如下:
1.在config.php开启hook
[php]
$config['enable_hooks'] = TRUE;
[/php]
2.hooks.php添加代码
[php]
$hook['display_override'] = array(
‘class’ => ‘MyDecorate’,
‘function’ => ‘init’,
‘filename’ => ‘MyDecorate.php’,
‘filepath’ => ‘hooks’
//’params’ => array(‘beer’)
);
[/php]
3、hooks目录新增文件MyDecorate.php
[php]
<?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
/**
* @author Zelipe
*
* 2012-05-26
*/
class MyDecorate {
private $CI;
public function __construct() {
$this->CI =& get_instance();
}
public function init() {
$html = ”;
// header html, ‘isAjax’ from helper
if(!isAjax())
$html .= $this->CI->load->view(‘header’, null, true);
// body html
$html .= $this->CI->output->get_output();
// footer html
if(!isAjax())
$html .= $this->CI->load->view(‘footer’, null, true);
$html = str_replace(‘ ‘, ”, $html); // 过滤代码缩进
$this->CI->output->_display($html);
}
}
[/php]
其中 isAjax() 为 helpers,可直接将此方法定义在MyDecorate.php内(可不作判断,如希望控制ajax不引入公用文件则保留)
[php]
// 是否为 ajax 请求
function isAjax() {
return (isset($_SERVER['HTTP_X_REQUESTED_WITH'])
&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === ‘xmlhttprequest’);
}
[/php]
4、views 目录新增header.php, footer.php。
四、采用layout的方式,即在一个主view里嵌入头尾;
在core目录下创建一个自定义的controller,代码如下:
[php]
class Frontend_Controller extends CI_Controller{
public function __construct(){
parent::__construct();
}
/**
* 加载视图
*
* @access protected
* @param string
* @param array
* @return void
*/
protected function _template($template, $data = array())
{
$data['tpl'] = $template;
$this->load->view(‘layout’, $data);
}
}
[/php]
创建一个主View, 命名为layout.php,
[php]
<!–header…–>
<div class="content">
<?php $this->load->view(isset($tpl) && $tpl ? $tpl : ‘default’); ?>
</div><!– end content –>
<!–footer…–>
[/php]
转载于:https://blog.51cto.com/11239102/1746230
CI学习 – header和footer相关推荐
- RecyclerView添加header与footer
前言 这次主要关于RecyclerView添加header和footer的实现方法,我们都知道,在使用ListView的时候我们能自由的给自己的ListView添加头部与尾部.使用addHeaderV ...
- HTML5的非主体结构元素(header、footer、hgroup、assress)
header元素 footer元素 hgroup元素 address元素 网页编排规则 1.header元素 代码演示: <!DOCTYPE html> <html lang=&qu ...
- Android listView 去掉header和footer中的分割线
Android listView 去掉header和footer中的分割线 方法一: 在listView中加上android:headerDividersEnabled="false&quo ...
- 隐藏JqueryMobile中的Header与Footer
<script type="text/javascript"> $(function () {//考虑你header和footer都存在var activePage = ...
- delphi xe6 android ListView增加 Header或Footer 的方法
var Item1: TListViewItem; begin Item1 := ListView1.Items.Add; Item1.Purpose:=TListItemPurp ...
- 实现带header和footer功能的RecyclerView——完善篇
在上一篇文章中我们实现了实现带header和footer功能的RecyclerView,见 实现带header和footer功能的RecyclerView 但是由于加入了header,item的po ...
- 实现带header和footer功能的RecyclerView
这个项目很简单,其实一年前就开发完成了,但是一直没闲下来去整理. RecyclerView是Android 5.0版本引入的一个新的组件,目的是在一些场景中取代之前ListView和GridView, ...
- GridView空记录时显示Header和Footer
也有段日子没写什么东西了,也是因为以前闲了好长时间,最近一下又有活干了,也不是什么新任务,还是接着原来没做完的工作重新设计和动工,对原来做好的曲线图形开发一个常用属性的设置,普通用户就可以通过页面对图 ...
- StroyBoard中UICollectionView中添加Header和footer
到Storyboard中,选择collection view controller中的"Collection View".在Attributes inspector中,选择&quo ...
- 纯静态网站模板封装header和footer
前后端分离的网站模板,如果不用任何渲染引擎,能否封装公共的header和footer(或其它html公共代码呢)? 答案是肯定的,因为jQuery有一个函数叫 load ,可以在浏览器绘制页面之前加载 ...
最新文章
- 从指定文件夹里COPY指定的一批文件列表(TXT文件)
- MapReduce 模式、算法和用例
- optee中的thread_vector_table线程向量表
- 删除github上某个release/tag
- 贵州师范学院数学与计算机科学,贵州师范学院数学与计算机科学学院
- 《彩虹坠入》如何用视觉元素增加游戏剧情表现力?
- 2017-12-04HTML布局_div布局
- 某些书籍翻译的太屎了,误导人!
- Android之安全退出应用程序的几种方式
- JDK,JRE,JVM三者关系
- java restrictions_Restrictions----用法
- 电子城西区北扩规划一路道路工程_雁塔区电子城街道重点项目进度
- Ubuntu 18.04/20.04 部署minikube
- 对计算机专业学科的认识1000字,计算机专业1000字实习报告
- 程序员是如何从小白做到年薪百万
- 电脑黑客用3D打印钥匙解开高安全性能手铐
- 优化策略5 Label Smoothing Regularization_LSR原理分析
- 高德地图使用(一)---添加自定义标注和标注事件
- 欧盟 GDPR 通用数据保护条例正式生效后,各行业影响分析
- SendMail.java