New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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

Comments

Projects
None yet
3 participants
@stephentcannon

stephentcannon commented May 25, 2012

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

This comment has been minimized.

Contributor

TomWij commented May 25, 2012

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

This comment has been minimized.

stephentcannon commented May 25, 2012

Strangely it is allowing me to do

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

Is this correct?

@TomWij

This comment has been minimized.

Contributor

TomWij commented May 25, 2012

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

@stephentcannon

This comment has been minimized.

stephentcannon commented May 25, 2012

two surrounding braces on each side

@TomWij

This comment has been minimized.

Contributor

TomWij commented May 25, 2012

@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

This comment has been minimized.

stephentcannon commented May 25, 2012

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

@dgreensp

This comment has been minimized.

Contributor

dgreensp commented May 30, 2012

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

This comment has been minimized.

Contributor

TomWij commented May 30, 2012

@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

This comment has been minimized.

Contributor

dgreensp commented Jun 5, 2012

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment