Welcome Guest, Not a member yet? Register   Sign In
Best way to implement dynamic site-wide menu?
#1

Hi,

Currently got a template to handle views, looks like this:

Code:
<?php $this->load->view('v_nav');?>
<div class="container-fluid">

<div id="header">
<?php $this->load->view('v_header');?>
</div>

<div id="main">
<?php $this->load->view($view);?>
</div>

<div id="footer">
<?php $this->load->view('v_footer');?>

the view 'v_nav' is simply hard-coded html of all the menu options.

So I'm considering putting the menu options in a table and generating the menu dynamically from that table.

I've got the code written to generate the necessary html from the table just trying to work out the best way to implement it.

Don't really want to have to pass another parameter into the template.

So, question is, is there a way to generate the html dynamically and have it available to echo out in the template?

Cheers
Reply
#2

(This post was last modified: 06-04-2018, 03:59 AM by InsiteFX.)

@Avenir has some very good tutorials, I would look through his whole web site.

Revisiting the multilevel menu in PHP with some additional code (as in… Bootstrap CSS)


Infinite dynamic Multi-level nested category with Codeigniter and MySQL
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 06-04-2018, 04:19 AM by blackbulldog.)

(06-04-2018, 03:54 AM)InsiteFX Wrote: @Avenir has some very good tutorials, I would look through his whole web site.

Revisiting the multilevel menu in PHP with some additional code (as in… Bootstrap CSS)


Infinite dynamic Multi-level nested category with Codeigniter and MySQL

Thanks for the links ... that's pretty much where I am at. I have all the code to create the menu.

What I haven't got is a way to implement it so I only do it once and the html is available to every view. Anyone help with that?

I think it might have something to do with extending using Mymodel and doing something in there ... but I'm VERY rusty with this stuff and can't recall how that might work!
Reply
#4

What I personally do is to have a _render() method in MY_Controller where I have custom bootstrapping code. It's simple, but gets the job done. Example:

PHP Code:
function _render($page$data = []) {
 
 if($this->uri->segment(1) != 'admin') {
 
   // load menu from DB or cache
 
   $this->load->model('pages_model''pag');
 
   $this->data['nav_pages'] = $this->pag->get_all(['active' => 1'nav' => 1]);
 
 }
 
 
  
// merge general data with view data
 
 $this->data array_merge($this->data$data);
 
 
  
// load content as string
 
 $this->data['rendered_page'] = $this->load->view($page$this->dataTRUE);
 
 
  
// load general app HTML with content inside it
 
 $this->load->view('templates/frontend'$this->data);


This is something I have used for many years now and it works just fine. I customize it for the needs of every project. There's also a bit more code that's not shown here in which I define a template (frontend or backend) so I use this function with the website as well as the admin panel.
Reply
#5

Thanks ...

... So, taking your idea, I could create a Mycontroller, create the html in the construct function (thereby only running it once) and then rather than using the code I have in my controllers now:

PHP Code:
$this->data['title'] = '';
 
$this->data['images'] = $images
 
$this->data['data'] = $output;
 
$this->data['view'] = 'v_home';
 
$this->load->view('v_template'$this->data); 

Simply have a simple render function like this in My_Controller:

PHP Code:
function _render($data = []) {
 
   $data['nav']=$this->html// $this->html created in construct function
 
   $this->load->view('v_template, $data);
  } 

and call it from my controller like:

PHP Code:
$this->data['title'] = '';
 
$this->data['images'] = $images
 
$this->data['data'] = $output;
 
$this->data['view'] = 'v_home';
_render($this->data

As I said I'm VERY rusty with this stuff so thanks for any help!!
Reply
#6

Just create nav.php view and have your menu code in it.

header
nav
body
footer
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

(06-04-2018, 06:37 AM)InsiteFX Wrote: Just create nav.php view and have your menu code in it.

header
nav
body
footer

Sorry, do you mean that I should try to access the model that returns the nav data inside my nav view?
Reply
#8

No assign it to the $data['nav'] then use a foreach loop to echo it all out.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#9

(06-04-2018, 09:15 AM)InsiteFX Wrote: No assign it to the $data['nav'] then use a foreach loop to echo it all out.

That would mean passing the $data['nav'] into the template view and changing the code every time the template view is loaded in the controllers, wouldn't it?
Reply
#10

Yes, but if there are changes to the menus like active etc; Then you would need to update it anyways.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB