Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
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
design
andjs
folders 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 calledThemeRoot
that 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: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->AddJsFile
doesnt seem to work for me. i've tried echoing the contents inside$Sender->AddJsFile
and 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_Before
function, it will show up before thehtml
tag - which is why you're using theAddJsFile
function. Take a look through theBase
controller if you'd like to get a better look at theAddJsFile
andAddCssFile
functions.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
ThemeRoot
variable and theBase_Render_Before
function 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