CodeIgniter Forums
Include a menu on every page? use url helper in a class? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Include a menu on every page? use url helper in a class? (/thread-24033.html)

Pages: 1 2


Include a menu on every page? use url helper in a class? - El Forum - 10-28-2009

[eluser]haloace[/eluser]
I'm new to CI and the MVC style. What's the best way to include a menu on every page? Can I use a ci helper in a class? Basically, I tried to create a class with my menu (using the url helper) and load it in the controller on each page, but it's not working.

//controller function
function index()
{
$this->load->library('Menu');
$menu = new Menu();

$this->load->view('header',$menu);
$this->load->view('home');
$this->load->view('footer',$menu);


}

//class
class Menu {

$this->load->helper('url');
$this->$url['home'] = anchor(base_url(), 'home', array('title' => 'home'));
$this->$url['about'] = anchor('about/', 'about', array('title' => 'about'));
$this->$url['forum'] = anchor('forum/', 'forum', array('title' => 'forum'));
$this->$url['faq'] = anchor('faq/', 'faq', array('title' => 'faq'));

}


Thanks for any help.


Include a menu on every page? use url helper in a class? - El Forum - 10-29-2009

[eluser]DogWin[/eluser]
我用jquery的click事件在同一页表达的.
menu
Code:
<a href="#" id="one">one</a>
<a href="#" id="two">two</a>
<a href="#" id="three">three</a>

<div id="show"></div>

js code like this:
$(document).ready(function(){
  $("#one").click(function(){
     $("#show").load('controller/function');
  });
});
希望能帮助你。


Include a menu on every page? use url helper in a class? - El Forum - 10-29-2009

[eluser]daparky[/eluser]
This should help you: http://codeigniter.com/wiki/Header_and_footer_and_menu_on_every_page/


Include a menu on every page? use url helper in a class? - El Forum - 10-29-2009

[eluser]John_Betong[/eluser]
&nbsp;
I have just written a common menu for our latest project. It checks the URL against the menu item title and if there is a match then the class changes to highlight the particular page item title.
&nbsp;

Code:
// CSS file
  #navigation a.on {color:#fff}
  #navigation a.off{color:#bbb}
  #navigation a    {display:inline; background:url(/images/nav.png);text-decoration:none}

  // _top_menu.php file declated in  MY_Controller.php
&lt;?php
  $a =    array
                (
                    'home'      => 'c_five/home',
                    'about'     => 'c_five/about',
                    'archives'  => 'c_five/archives',
                    'contact'   => 'c_five/contact',
                    'writing'   => 'c_five/writing',
                    'subscribe' => 'c_five/subscribe'
                );
    foreach($a as $title => $url):
        $toggle = strpos($this->uri->uri_string, $url) ? 'on' : 'off';
        echo anchor($url, $title, "class='$toggle'") .nbs(15);
    endforeach;                
    unset($toggle);unset($a);
//
&nbsp;
&nbsp;
&nbsp;
edit: spelling and formatting


Include a menu on every page? use url helper in a class? - El Forum - 10-29-2009

[eluser]haloace[/eluser]
Thanks all for your input. Learning new techniques is always good. I ended up going for a simple approach.
Code:
//controller
$this->load->helper('url');
include("menu.inc");

//menu.inc
<div>
<a href="&lt;?php base_url() ?&gt;;" title="Home Page">Home</a>
<a href="/forum/" title="Drop us a note.">Forum</a>
<a href="/about/" title="Learn about us">About</a>
</div>



Include a menu on every page? use url helper in a class? - El Forum - 10-30-2009

[eluser]jedd[/eluser]
[quote author="haloace" date="1256893585"]
Learning new techniques is always good.
Code:
include("menu.inc");
[/quote]

I'd suggest this is actually a very old technique.

And I'd strongly recommend you don't do this. At the very least turn it into a helper.

If you want to learn a new technique, and actually take advantage of CodeIgniter's features, use Colin's templating system, or the view-preparation in MY_Controller approach.


Include a menu on every page? use url helper in a class? - El Forum - 10-30-2009

[eluser]haloace[/eluser]
[quote author="jedd" date="1256921226"][quote author="haloace" date="1256893585"]
Code:
include("menu.inc");
[/quote]

I'd suggest this is actually a very old technique.

And I'd strongly recommend you don't do this. At the very least turn it into a helper.
[/quote]

What is the disadvantage of this technique?


Include a menu on every page? use url helper in a class? - El Forum - 10-30-2009

[eluser]NateL[/eluser]
[quote author="John_Betong" date="1256844243"]&nbsp;
I have just written a common menu for our latest project. It checks the URL against the menu item title and if there is a match then the class changes to highlight the particular page item title.
&nbsp;

Code:
// CSS file
  #navigation a.on {color:#fff}
  #navigation a.off{color:#bbb}
  #navigation a    {display:inline; background:url(/images/nav.png);text-decoration:none}

  // _top_menu.php file declated in  MY_Controller.php
&lt;?php
  $a =    array
                (
                    'home'      => 'c_five/home',
                    'about'     => 'c_five/about',
                    'archives'  => 'c_five/archives',
                    'contact'   => 'c_five/contact',
                    'writing'   => 'c_five/writing',
                    'subscribe' => 'c_five/subscribe'
                );
    foreach($a as $title => $url):
        $toggle = strpos($this->uri->uri_string, $url) ? 'on' : 'off';
        echo anchor($url, $title, "class='$toggle'") .nbs(15);
    endforeach;                
    unset($toggle);unset($a);
//
&nbsp;
&nbsp;
&nbsp;
edit: spelling and formatting[/quote]

I was JUST trying to figure this part of my site out. Very helpful code for me! Thanks a bunch!

For some reason tho, every instance that gets echoed out in the foreach statement is in the "off" state. Any ideas why?

Code:
$a =    array
      (
          'home'          => '/home',
          'about'         => '/about',
          'portfolio'      => '/portfolio',
          'contact'       => '/contact'
      );
    
    foreach($a as $title => $url):
        $toggle = strpos($this->uri->uri_string, $url) ? ' class="active"' : '';
        echo "<li $toggle>".anchor($url, $title)."</li>";
    endforeach;                
    unset($toggle);
    unset($a);



Include a menu on every page? use url helper in a class? - El Forum - 10-30-2009

[eluser]InsiteFX[/eluser]
Looks like you need to setup the CSS file and the navigation divisions in code.

Enjoy
InsiteFX


Include a menu on every page? use url helper in a class? - El Forum - 10-30-2009

[eluser]NateL[/eluser]
[quote author="InsiteFX" date="1256984997"]Looks like you need to setup the CSS file and the navigation divisions in code.

Enjoy
InsiteFX[/quote]

My CSS is set up and working properly. I tested this by swapping the "class='active'" to the "off" position and they all lit up as "active"

if I put in this line of code:

echo $this->uri->uri_string;

it echos "about" - so I'm expecting to see my About page with the active list item, but it doesn't work for me.