开发者

Codeigniter How to Dynamically create menu

So I have a menu that I want to generate a main menu based on the authenticated user's access level. No problem creating the menu, however I want to automatically create the generated menu in my "header" view. So in my controller I am calling the "header" view, but I don't want to pass this dynamic part of the header like this:

$data['menu'] = 'Some Generated HTML Menu';
$this->load->view('header',$data);

I would rather it already be included in my header file, but I'm not exactly sure how to do this (as开发者_开发技巧ide from adding the $data declaration from inside of my constructor).


I don't know if this will help you, but this was how I did my template system for the longest time before moving on to a more advanced method.

Top Head: views/inc/top_head.php

<html>
<head>
<!-- all of your imports you want across all pages -->

Bottom Head: views/inc/bottom_head.php

I do it this way so that I can split and add custom Javascript things and maybe bring in special case imports.

</head>
<body>
<div id="main_container">
    <div id="navigation">
         <?php
              // DO YOUR NAVIGATION MAGIC HERE
              if($is_logged_in) :
                  // BAM MAGIC DONE
              else :
                  // No magic show here
              endif;
         ?>
    </div>

Footer: views/inc/footer.php

This is where you will put in your footer things etc....

</div>
</body>

Now we are at the point that we need to actually fill content in to the template

Index Page: /views/some_controller/index.php

<?php $this->load->view('inc/top_head.php'); ?>

<?php $this->load->view('inc/bottom_head.php;) ?>

<h1>Hello</h1>
<p>Some filler content and stuff I guess would go here...Of course</p>

<?php $this->load->view('inc/footer.php'); ?>

So there, we have a quick template system. Now to show you what I have done for a controller

<?php

class Some_Controller extends Controller {
    public $page_data;

    public function __construct() {
        parent::__construct();       // Load parent constructor

        // This is page data that we obviously don't want to keep retyping
        $this->page_data = array(
            'is_logged_in' => FALSE, // Obviously do some test here
            'page_title'   => 'Some Title'
        );
    }

    public function index() {
        $this->_load('some_controller/index');
    }

    /** Should think of a better name but meh */
    private function _load($view) {
        $this->load->view($view, $this->page_data);
    }
}

I hope this helped in some way or another. Mind you this is a quick write up. If I really wanted this to go into production, I would have moved the _load function into a parent class and extended it. I would also probably move the page_data variable along with it.


You could just make $data['menu'] = an multi-dimension array of (button_name, url)'s. Then in the view you pass this array to a plugin which generates the html menu based on that array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜