在模板工具包中转义单引号
您是否曾在模板工具包中转义单引号以获取必要的 JavaScript 处理程序?如果是这样,你该怎么做。
[% SET s = "A'B'C" %]
<a href="/abc.html" onclick="popup('[% s | html_entity %]')">ABC</a>
html_entity
显然不起作用,因为它只处理双引号。那么你该怎么做呢?
Do you ever escape single quotes in template toolkit for necessary javascript handlers? If so, how do you do it.
[% SET s = "A'B'C" %]
<a href="/abc.html" onclick="popup('[% s | html_entity %]')">ABC</a>
html_entity
obviously doesn't work because it only handles the double quote. So how do you do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不使用内联事件处理程序——出于同样的原因,我拒绝使用 css 的
style
属性。 Jquery 只是让在 html 和$('.foo').click( function () {} )
上轻松执行class="foo"
,在外部.js
文件。但是,为了尽力回答这个问题,请查看 这些文档在
Template::Filter
上查看核心中的内容。看来你可以做
[%s | Replace( "'", "\\'" ) %]
,转义单引号。或者您可以编写一个更复杂的清理 JavaScript 解析器,只允许函数调用,并制作您自己的 模板::过滤器I don't use the inlined event handlers -- for the same reason I refuse to use the
style
attribute for css. Jquery just makes it to easy to doclass="foo"
on the html and$('.foo').click( function () {} )
, in an external.js
file.But, for the purpose of doing my best to answer this question, check out these docs on
Template::Filter
for the ones in core.It seems as if you could do
[% s | replace( "'", "\\'" ) %]
, to escape single quotes. Or you could probably write a more complex sanitizing javascript parser that permits only function calls, and make your own Template::Filter2018 年更新供参考:
TT 有一个方法,称为 sqote 用于转义单引号,dquote 用于转义双引号。
有问题的链接类似于:
http://www.template-toolkit。 org/docs/manual/VMethods.html#section_squote
2018 update for reference:
TT has a method for this called squote for escaping single quotes and dquote for double quotes.
Questioned link would be something like:
http://www.template-toolkit.org/docs/manual/VMethods.html#section_squote
您可以尝试:
popup('[%s | html %]')
。You can try:
popup('[% s | html %]')
.Perl 不是我最强的语言...但是!
我发现的最简单的方法是使用
JSON
模块。在名为JS.pm
或其他内容的模块中:更多信息:http://search.cpan.org/~makamaka/JSON-2.90/lib/JSON.pm
然后在您的模板中:
Perl isn't my strongest language... But!
Easiest way I've found is to use the
JSON
module. In a module calledJS.pm
or something:More here: http://search.cpan.org/~makamaka/JSON-2.90/lib/JSON.pm
Then in your template:
请记住在替换中对斜杠进行双重转义,否则将被解释为转义撇号。
Remember to double-escape the slash in the replacement, otherwise it will be interpreted as escaping the apostrophe.