How to properly use class theme hooks?
fr3em1nd
✭✭
HI,
I'm pretty new to class theme hooks, what's happening now is i have created a whole new template and trying to retain .TPL as much as possible, so i started using theme hooks.. im Using Vanilla 2.1
i created a new file in the theme directory : class.MyThemeNamethemehooks.php
what i want to accomplish is to add some JS files before </head> tag
i tried this:
<?php if (!defined('APPLICATION')) exit();
class MyThemeNameThemeHooks implements Gdn_IPlugin {
public function Setup() {return TRUE;}public function OnDisable() {return TRUE;}
}
public function Base_Render_Before($Sender) {
$Sender->AddJsFile('dialog.js','/js/modern/');
}}
i added this: $Sender->AddJsFile('dialog.js','/js/modern/');
but no go... is there anything i missed ??
Tagged:
0
Comments
Slight miss: Theme hooks only looks for CSS and JS directly within the
designandjsfolders when only a file name is passed for some reason, meaning that you cannot define the path the same way you would in plugins (/js/modern/). What I usually do, is define a global variable calledThemeRootthat outputs the path to the theme directory. This way, you can add assets located anywhere within your theme without having to worry about installation structure and folder naming:$GLOBALS["ThemeRoot"] = str_replace($_SERVER["DOCUMENT_ROOT"], "", __DIR__); class [Whatever]ThemeHooks implements Gdn_IPlugin { public function Base_Render_Before($Sender) { /** * Don't want no nasty files in my dashboard */ if (InSection("Dashboard")) return; /** * Store the path to the js folder */ $ScriptsRoot = $GLOBALS["ThemeRoot"]."/js"; /** * Add away! */ $Sender->AddJsFile($ScriptsRoot."/modern/dialog.js"); } }Kasper Kronborg Isager (kasperisager) | Freelance Developer @Vanilla | Hit me up: Google Mail or Vanilla Mail | Find me on GitHub
hi @kasperisager , are you sure we need to put it in a Base_Render_Before function ? i've tried exploring it and
$Sender->AddJsFiledoesnt seem to work for me. i've tried echoing the contents inside$Sender->AddJsFileand it's being rendered before<html>tag...I practically copy/pasted the code I'm using in one of my themes so yes, I'm pretty sure :-) And yes, if you echo stuff inside the
Base_Render_Beforefunction, it will show up before thehtmltag - which is why you're using theAddJsFilefunction. Take a look through theBasecontroller if you'd like to get a better look at theAddJsFileandAddCssFilefunctions.Kasper Kronborg Isager (kasperisager) | Freelance Developer @Vanilla | Hit me up: Google Mail or Vanilla Mail | Find me on GitHub
@kasperisager T_T it's not really working , i dont know what i missed, im using 2.1a334
Here's what might be wrong: If you copied the code from above and pasted it directly in your theme hooks, it won't work. What you need to do, is add the
ThemeRootvariable and theBase_Render_Beforefunction to your own theme hooks as the example I posted is incomplete.Kasper Kronborg Isager (kasperisager) | Freelance Developer @Vanilla | Hit me up: Google Mail or Vanilla Mail | Find me on GitHub
@kasperisager thanks