使用codeignitor实现动态菜单/页眉/页脚的最佳方式

问题描述:

我只是想知道使用codeignitor实现菜单,页眉和页脚更改内容(如通知)的最佳方式/练习。使用codeignitor实现动态菜单/页眉/页脚的最佳方式

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

我想我需要一个全局的方式来调用函数,从任何控制器加载网站标题(菜单),它可以计算出内容并相应地显示视图。

因此例如显示博客页面的控制器。
在你的控制器构造 - 定义一个方法,你的博客查看文件所在的文件夹和模板名称

// 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); 

模板本身 意见/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'); 
+0

不错,我喜欢有一个处理所有事情的通知模型的想法:) – user2321428

+0

谢谢 - 是的,我认为它的重要性在于从不直接从视图文件调用模型,所以这是一个体面的妥协。当然你也不必使用$ this-> view_template - 你可以直接通过它的名字来调用模板:$ this-> load-> view('blog_template',$ data);我只是想在控制器的顶部定义模板。 – cartalot