Skip to content
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

@stephentcannon
Copy link

@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);

@TaraWij
Copy link
Contributor

@TaraWij TaraWij 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
Copy link
Author

@stephentcannon stephentcannon commented May 25, 2012

Strangely it is allowing me to do

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

Is this correct?

@TaraWij
Copy link
Contributor

@TaraWij TaraWij commented May 25, 2012

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

@stephentcannon
Copy link
Author

@stephentcannon stephentcannon commented May 25, 2012

two surrounding braces on each side

@TaraWij
Copy link
Contributor

@TaraWij TaraWij 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
Copy link
Author

@stephentcannon stephentcannon commented May 25, 2012

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

@dgreensp
Copy link
Contributor

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

@TaraWij
Copy link
Contributor

@TaraWij TaraWij 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
Copy link
Contributor

@dgreensp 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
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants