Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebars helper - returning HTML not text

Tags:

I wrote a simple helper for my template. Here's the code:

Handlebars.registerHelper('splitQuote', function (string) {     if (string.indexOf('|') !== -1) {         return string.replace('|', '<span>') + '</span>';     }     return string; }); 

So I pass a string, and split the string by '|' character. I also want to put second part into span tags.

Problem is, the result that is being returned is pure text, so I get span tags like a text, not HTML.

Does anyone know what's the catch?

Tnx

like image 870
Adrian Avatar asked Nov 20 '13 19:11

Adrian


People also ask

How do you use ternary Handlebars?

The if helper can be used as a ternary operator by passing three arguments to it. In the following example, a button has a default value of "Save Changes" , but when model. isSaving is true, then the value temporarily changes to Saving... .

Is there else if in Handlebars?

Handlebars supports {{else if}} blocks as of 3.0. 0. is the point in not having an elsif (or else if) in handlebars that you are putting too much logic into your template.

Which built in helper method is the inverse of `# If `?

You can use the unless helper as the inverse of the if helper.

What are the helpers in handlebars?

Helpers in handlebars are function which will perform a certain action on given input & returns with the output. Some of the in-built helpers are #if, #unless, #each, #with, log & lookup. It is an inbuilt block helper, used to check conditional statements in handlebars. If the value is false the content inside the if block is rendered.

How do I block helpers in handlebars conditional statements?

When writing a conditional, you will often want to make it possible for templates to provide a block of HTML that your helper should insert if the conditional evaluates to false. Handlebars handles this problem by providing generic else functionality to block helpers.

How do you pass parameters to a helper in handlebars?

Parameters are passed to helpers in the order that they are passed, followed by the options hash. A common use-case for block helpers is using them to define custom iterators. In fact, all Handlebars built-in helpers are defined as regular Handlebars block helpers.

What is handlebars extend in JavaScript?

Handlebars.Utils.extend (foo, {bar: true}) Will set the key bar on object foo with the value true. Generic toString method. Determines if an object is an array. Determines if an object is a function. Logger used by the log helper. May be overridden if desired.


2 Answers

You don´t need to use SafeString. Instead, use the "triple moustaches" from handlebar:

From Handlebars web site, HTML Escaping section:

Handlebars HTML-escapes values returned by a {{expression}}. If you don't want Handlebars to escape a value, use the "triple-stash", {{{.

So, a simple triple quote in your html will avoid escaping:

{{{splitQuote}}}  
like image 53
Mendes Avatar answered Nov 14 '22 02:11

Mendes


You have to mark the string as html in your helper if you want to Handlebars not to escape it. Use Handlebars.safeString to do this. The below should suit your needs

Handlebars.registerHelper('splitQuote', function(string) {     if (string.indexOf('|') !== -1) {         return new Handlebars.SafeString(string.replace('|', '<span>') + '</span>');     }     return string; }); 

As mentioned in comments you should probably escape the passed string using Handlebars.Utils.escapeExpression(string) to encode the string before you do your custom formatting. I'd recommend writing like this:

Handlebars.registerHelper('splitQuote', function(string) {     string = Handlebars.Utils.escapeExpression(string);     if (string.indexOf('|') !== -1) {         string = string.replace('|', '<span>') + '</span>';     }     return new Handlebars.SafeString(string); // mark as already escaped }); 
like image 21
megawac Avatar answered Nov 14 '22 04:11

megawac