Handlebars.compile is not a function?

(Steven Slade) #1

I have a handlebars template for my site’s nav and I am getting that Handlebars.compile is not a function, por qué?

Uncaught TypeError: Handlebars.compile is not a function

   import { withPluginApi } from 'discourse/lib/plugin-api';

    export default {
      name: "notification-menu-item",

      initialize() {

        withPluginApi('0.1', api => { 

          $(document).ready(function() {
            var source                 = $("#notification-menu-item").html();
            var template               = Handlebars.compile(source); //not a function?
            var user                   = Discourse.User.current(); //api.getCurrentUser();
            var logged_user            = false;
            var pro                    = false;
            var notification_url       = "";
            var data_url               = "";
            var new_notification_class = "";
            var notification_count     = "";

How to pass a variable from a plugin's initializer to an .hbs file?
(Sam Saffron) #2

You need to precompile your handlebars, we don’t ship the compiler anymore which saves 20k or so client side

(Robin Ward) #3

Also, if you have the ability to export an initializer, you can just include the handlebars file in your plugin and it will be compiled automatically.

(Steven Slade) #4

Would you mind elaborating on this?

(Robin Ward) #5

If you just include a .hbs file in your plugin, it will be compiled as part of the rails asset pipeline. I’m not sure why your plugin is trying to call Handlebars.compile directly, but you shouldn’t have to do that.

(Steven Slade) #6

I guess it’s pertinent to note this was to show nav items in the customize Header in the Discourse UI. The plugin was sending the template to this script in the Header.

<script id="notification-menu-item" type="text/x-handlebars-template">
    <div id="navmenucontainer" class="container">
        <div id="navmenuv">
            <ul class="nav">
              {{#if logged_user}}
                {{#if pro}}
                    <li>My Data</li>

Would the correct way of doing this now be to just include this script as an.hbs file in the plugin (ex: ../templates/navigation/header.hbs)?

And then in my initializer, var template would equal to the .hbs?

(Steven Slade) #7

@eviltrout and @sam: So I’m struggling a bit with this. I need variables like pro (which is a boolean) passed to my .hbs (which ultimately makes up my nav bar items in the header). Previously, I was achieving this with Handlebars.compile(source) and then passing the needed variable to the compiled template. Not really sure how to go about this now.