Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

javascript - 特定于把手-传递把手表达式时转义单引号和双引号(Handlebars specific - escape both single and double quotes when passing Handlebars expression)

HTML and Handlebars:(HTML和车把:)

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.(必须定义一个JS变量并将其传递给反斜杠加法器感觉很草率。) Is there a cleaner way to do this with Handlebars or Moustache?(有没有更清洁的方法来使用把手或小胡子呢?)   ask by Kate translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/(我写了一个简单的示例在jsfiddle上进行演示: http : //jsfiddle.net/VLy4L/)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...