Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
40
rated 0 times [  44] [ 4]  / answers: 1 / hits: 18185  / 10 Years ago, fri, february 28, 2014, 12:00:00

HTML and Handlebars:



onclick='shareItem({{name}})'> 


Does not successfully pass a safely escaped name when it has double quotes in it.



onclick=shareItem('{{name}}')> 


Does not successfully pass a safely escaped name when it has single quotes in it.



I need to handle both eventualities- and even in the same string.



It feels sloppy to have to define a JS variable and pass it to a backslash adder.



Is there a cleaner way to do this with Handlebars or Moustache?


More From » html

 Answers
9

You need to register a inline helper that manipulates the context. In your case, you need to escape a single or double quote.



Handlebars.registerHelper('escape', function(variable) {
return variable.replace(/(['])/g, '\$1');
});


By registering such helper, you can use it with a variable to achieve what you want.



{{ escape name }} # expects to escape any ' or 


I wrote a simple example to demonstrate this on jsfiddle: http://jsfiddle.net/VLy4L/


[#72232] Thursday, February 27, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
miles

Total Points: 256
Total Questions: 111
Total Answers: 104

Location: Benin
Member since Fri, Mar 24, 2023
1 Year ago
;