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

stephentcannon opened this Issue May 25, 2012 · 9 comments


None yet

3 participants


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


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

{{#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);


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


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


Strangely it is allowing me to do


Is this correct?


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


two surrounding braces on each side


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


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


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.


@dgreensp: According to the Helpers part under 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.


@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