return new Handlebars.SafeString(result); throws Uncaught TypeError: undefined is not a function #160

Closed
stephentcannon opened this Issue May 25, 2012 · 9 comments

Projects

None yet

3 participants

@stephentcannon

Trying to return HTML markup into a Template from a Handlebars helper throws an Uncaught TypeError

Helper

Handlebars.registerHelper('testme', function() {
    var a = '<div class="span2"><b>name</b></div><div class="span2"><b>value</b></div><div class="span2"><b>delete</b>   </div><br />';
    console.log('a: ' + a); //a is valid
    // return a; //uncomment this line and comment next to return a as text 
    return new Handlebars.SafeString(a); 
});

Template
{{#testme }}{{/testme}}

Result in browser console
Uncaught TypeError: undefined is not a function

This appears to be offending statement because you can return 'a' without safestring method call and it works fine except that the content returned is not HTML and displayed as raw text.
return new Handlebars.SafeString(a);

@TomWij

I've found SafeString to not be supported by Handlebars, you can instead do:

{{{#testme}}}

As I haven't used a helper that way I don't know if the closing tag is necessary, then it would be:

{{{#testme}}}{{{/testme}}}
@stephentcannon

Strangely it is allowing me to do

{{#testme}}{{/testme}}

Is this correct?

@TomWij

@stephentcannon: Uhm, just to be sure, with two surrounding {} or three surrounding {}?

@stephentcannon

two surrounding braces on each side

@TomWij

@stephentcannon: I haven't actually seen any example that shows you can {{#...}}{{/...}} a helper, it's usually done in the context of an {{#if ...}}{{else}}{{/if}}. You probably mean to use the usual {{...}} instead, but then it wouldn't be a helper.

In any case, two comments ago I've shown a way to make sure the HTML isn't getting escaped.

@stephentcannon

Yep, thank you very much. This one goes in my list of things never to forget.

@dgreensp

We don't have Handlebars.SafeString in Meteor at the moment.

This syntax: {{#foo}}blah blah blah{{/foo}} is for a "block helper" foo, where you want the contents to be passed as an argument to the helper. If you don't need to pass block contents, just use {{foo}} (or {{{foo}}} if foo returns raw HTML).

registerHelper is useful if you need to share a helper between multiple templates; otherwise you can just assign the helper to the template.

I suppose the point of SafeString is that you don't have to be diligent about calling a helper with the right number of curly braces.

@TomWij

@dgreensp: According to the Helpers part under http://handlebarsjs.com/expressions.html I would assume that {{{...}}} and Handlebars.SafeString(...) would have to be used together in order to work, feel free to double check before implementing something.

@dgreensp

@TomWij: Well, {{{...}}} prevents escaping on its own, and so must Handlebars.SafeString(...) to be useful. I think the intention is for defensive coding. If your helper returns HTML, then you don't ever want it to be escaped, no matter how it's called. The docs don't really point this out, though.

@dgreensp dgreensp closed this Jun 19, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment