ericf / express-handlebars

A Handlebars view engine for Express which doesn't suck.
BSD 3-Clause "New" or "Revised" License
2.31k stars 384 forks source link

Missing Helper #262

Closed haswent closed 5 years ago

haswent commented 5 years ago

I have a weird issue where my text/x-handlebars-template cannot see helpers but the main .hbs file can.

Creating hbs like this passing in helpers:

    var hbs = exphbs.create({
        helpers: hbs_helpers,
        extname: '.hbs',
        defaultLayout: 'main'
    });

Example of a helper being passed in

exports.if_eq = function(a, b, opts) {
    if (a == b) return opts.fn(this);
    else return opts.inverse(this);
};

I'm using this within the main .hbs file and it works correctlly however i'm also updating some data using and API call and using the below tag to store the hbs template to call later

<script type="text/x-handlebars-template" id="grid_view_template">... </script>
    var source = $("#grid_view_template").html();
    var template = Handlebars.compile(source);
    var html = template({
        vehicles: data.vehicles
    });

However i'm getting an error within my template that:

Error: Missing Helper 'if_eq'

But i'm using the if_eq helper in lots of places within my template and it works fine, not sure if a particular library has been updated in node but it has stopped working when it used to work previously

DjovDev commented 5 years ago

Hello, have you found a solution for this? I have the same problem here.

haswent commented 5 years ago

Yeah - So the helpers don't get shared within the templates on the client side, so i've had to manually register the helpers on the client side for now (Will look at using a build time script to do this in the future)

Example:

<script>
    Handlebars.registerHelper('if_eq', function (a, b, opts) {
        if (a == b) return opts.fn(this);
        else return opts.inverse(this);
    });

    Handlebars.registerHelper('ToLowerCase', function (str) {
        return str.toLowerCase();
    });
</script>