使用codeignitor实现动态菜单/页眉/页脚的最佳方式 [英] Best way to implement dynamic menus/headers/footer using codeignitor

查看:67
本文介绍了使用codeignitor实现动态菜单/页眉/页脚的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道实现菜单,页眉和页脚与更改内容,如通知使用codeignitor的最佳方式/实践。

I was just wondering the best way/practice to implement menus, headers and footers with changing content such as notifications using codeignitor.

例如,我有一个警报在头文件菜单中,链接回数据库中的数据,我需要检查更改每次加载页面。最初,我想我可以使用$ this-> load-> view('header')每次调用头,但这意味着我需要一个全局函数来处理警报的任何更改,然后传递到标题视图,每次,不好!

For example say I had an alert within the header menu that linked back to data within a database and I needed to check for changes each time a page is loaded. Initially I thought I could call the header using $this->load->view('header') each time, but this would mean I would need a global function to work out any changes on alerts and then pass that to the header view, each time, not good!

我想我需要一个全局的方法来调用从加载网站标题(菜单)的功能从任何控制器,工作出的内容和显示相应的视图。

I guess I need a global way to call function that loads the website header (menu) from any controller which works out the content and displays the view accordingly.

推荐答案

因此,例如显示博客页面的控制器。

在控制器构造函数中 - 您的博客视图文件和模板名称

so for example a controller that shows blog pages.
in your controller constructor - define the folder your blog view files are in and the template name

// the folder your content files are in 
$this->templatefolder = 'blog' ;
// the template name
$this->view_template = 'blog_template' ; 

$data['content01'] = 'search_articles';
$data['content02'] = 'main_article';
$data['content03'] = 'suggested_articles';
$this->load->view( $this->view_template, $data ); 

模板本身
views / blog_template.php

the template itself views/blog_template.php

// opening html etc that is generic to website
$this->load->view('tmpl_open');

// so if the header has to be dynamic
// get the header from a model (or library etc) 
// and either pass the header content or just echo it out directly 
$this->load->model('header');

if( ! $newHeader = $this->header->returnNewHeader() )
{

  // fallback if the header doesn't come back from the model 
  $this->load->view('default_header');

} 
else
{   echo $newHeader ;     } 

// this is optional but IF the template folder is not set 
// we have a default folder called 'pages' to look in for the content views 

// but in this example the folder is set to be 'blog' 
// so the blog view files will be in application/views/blog/search_articles.php etc etc
if( isset($this->templatefolder)){

$templatefolder = $this->templatefolder . '/' ;  }

else { $templatefolder = 'pages/'; }

// header that is specific for the content 
$this->load->view($templatefolder . 'header');

// so in this specific example its going to load 3 view files, but this part is completely flexible 
if(isset($content01))
$this->load->view($templatefolder.$content01);

if(isset($content02))
$this->load->view($templatefolder.$content02);

if(isset($content03))
$this->load->view($templatefolder.$content03);

if(isset($content04))
$this->load->view($templatefolder.$content04);

if(isset($content05))
$this->load->view($templatefolder.$content05);

if(isset($content06))
$this->load->view($templatefolder.$content06);

if(isset($content07))
$this->load->view($templatefolder.$content07);

if(isset($content08))
$this->load->view($templatefolder.$content08);

// example of an optional file that you can uncomment for testing 
// $this->load->view('objecttesting');

// bottom nav bar generic to website
$this->load->view('tmpl_footer');

// closing html etc generic to website
$this->load->view('tmpl_close'); 

这篇关于使用codeignitor实现动态菜单/页眉/页脚的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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