Best way to implement dynamic site-wide menu? |
Hi,
Currently got a template to handle views, looks like this: Code: <?php $this->load->view('v_nav');?> 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
@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 )
(06-04-2018, 03:54 AM)InsiteFX Wrote: @Avenir has some very good tutorials, I would look through his whole web site. 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!
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 = []) { 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.
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'] = ''; Simply have a simple render function like this in My_Controller: PHP Code: function _render($data = []) { and call it from my controller like: PHP Code: $this->data['title'] = ''; As I said I'm VERY rusty with this stuff so thanks for any help!!
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 )
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 )
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 )
|
Welcome Guest, Not a member yet? Register Sign In |