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?

fr3em1ndfr3em1nd ✭✭
edited August 2013 in Vanilla 2.0 - 2.8

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:

Comments

  • KasperKasper Scholar of the Bits Copenhagen Vanilla Staff
    edited February 2013

    Slight miss: Theme hooks only looks for CSS and JS directly within the design and js 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 called ThemeRoot 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:

    $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->AddJsFile doesnt seem to work for me. i've tried echoing the contents inside $Sender->AddJsFile and it's being rendered before <html> tag...

  • KasperKasper Scholar of the Bits Copenhagen Vanilla Staff

    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 the html tag - which is why you're using the AddJsFile function. Take a look through the Base controller if you'd like to get a better look at the AddJsFile and AddCssFile 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

  • KasperKasper Scholar of the Bits Copenhagen Vanilla Staff
    edited February 2013

    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 the Base_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

Sign In or Register to comment.