How to add a navbar above navbar?
Hey guys,
so i'm trying to write (emphasis on trying) a custom plugin that will add a navbar above current navbar.
And honestly i'm lost.. my PHP skills are none but i do have some knowledge of programming.
I'd appreciate a lot every hint/direction of where to start.
I think i figured out that i have to use function Base_render_before and $Sender->smth->AddString('some html here')
where smth is something that i have no idea what.. a hook that i should use?
Anyway..help appreciated
I'm using Vanilla 2.1 and Vanilla bootstrap theme (united).
Best Answers
-
hgtonight MVP
Welcome to the community!
Looking at Bootstrap's master template, there is no asset for your plugin to hook. I would suggest adding your markup to your
/themes/bootstrap/views/default.master.tpl
file.This is, generally, the best way to insert static content that will be on every page. If you need the extra navbar to be dynamic, we can go from there.
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
5 -
hgtonight MVP
After reviewing my previous post, and reading up on the autoloader class, I appear to have mislead you. It appears that themes can't autoload modules.
What this means is you need to include the module file manually.
Replace your themehooks file with the following:
<?php if(!defined('APPLICATION')) exit(); include_once('modules/class.dynamicnavbarmodule.php'); class BootstrapThemeHooks implements Gdn_IPlugin { /** No setup required. */ public function Setup() { } public function Base_Render_Before($Sender) { $Sender->AddModule('DynamicNavbarModule'); } }
You also need to change your module code to the following:
<?php if(!defined('APPLICATION')) exit(); /** * Renders a dynamic navbar */ class DynamicNavbarModule extends Gdn_Module { public function __construct($Sender = '') { parent::__construct($Sender); $this->SetData('Menu', 'this is my sweet new navbar!'); } public function AssetTarget() { return 'AboveNavbar'; } public function ToString() { $Menu = $this->Data('Menu'); if($Menu) { echo $Menu; } } }
Let me know if you find any other issues with this example code.
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
5
Answers
Welcome to the community!
Looking at Bootstrap's master template, there is no asset for your plugin to hook. I would suggest adding your markup to your
/themes/bootstrap/views/default.master.tpl
file.This is, generally, the best way to insert static content that will be on every page. If you need the extra navbar to be dynamic, we can go from there.
Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
hgtonight hi and thanks!
Yeah thats my backup plan (default.master.tpl) but not a very good one for me because my intention is to pull page titles and links from a database..
And smartys php tag is deprecated now so.. any other ideas?
Thanks!
EDIT - Please read the follow up to this post for corrected code.
For dynamic content, I suggest you add a new asset, create a module that contains your logic, and then add this module to every page.
/themes/bootstrap/views/default.master.tpl
file using the smarty tag{asset name="AboveNavbar"}
.Create a file in
/themes/bootstrap/modules
calledclass.dynamicnavbarmodule.php
and paste in the following code:Add the module to every page with the following code in your
/themes/bootstrap/class.bootstrapthemehooks.php
file:If all went well, you should see your 'this is my sweet new navbar!' text above your navbar.
Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
Well i tried your code and its not working for me.. page loads fine but no 'sweet new navbar' message is shown
After reviewing my previous post, and reading up on the autoloader class, I appear to have mislead you. It appears that themes can't autoload modules.
What this means is you need to include the module file manually.
Replace your themehooks file with the following:
You also need to change your module code to the following:
Let me know if you find any other issues with this example code.
Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
This one works like a charm! Thanks a lot!