使用HTML实体转义的Ruby on Rails Slim模板

3

I have a RoR slim template with the following:

input(type="text" placeholder="I'm looking for…")

但是遗憾的是,它会输出HTML实体转义字符'原样',即字符串文字。我正在使用Rails gem 'slim'来渲染输入字段。

希望输出带有省略号字符的结果...

<input type="text" placeholder="I'm looking for…">

实际输出

<input type="text" placeholder="I'm looking for&hellip;">

我已经尝试在结尾处添加html_safe调用,但没有成功,例如:
input(type="text" placeholder="I'm looking for&hellip;").html_safe

非常感谢。

你能发布原始输出和期望输出吗? - Pavan
3个回答

3

自己解决,简单的解决方案,显而易见的答案 - 需要一组括号:

input(type="text" placeholder=("I'm looking for&hellip;".html_safe))

只是一个提示:使用“raw”或可能甚至使用“sanitize”方法是“首选”的方式。目前,“raw”比“html_safe”的唯一优点是,在将对象标记为“html_safe”之前,它会调用to_s方法,因此它适用于任何可以转换为字符串的内容,而不仅仅是字符串。我猜它在未来可能会做更复杂的事情。 - Inkling
另外一点需要注意的是:“括号其实是不必要的”。之前你在input上调用了html_safe,这是不正确的。现在你需要在字符串上调用它,然后将其作为占位符属性的值放入其中。 - ARK

0
根据Slim的网站,默认情况下文本已被转义。为了防止转义,请在文本前加上双等号符号。因此(不太直观地),
input(type="text" placeholder=="I'm looking for&hellip;")

应该能够正常工作。

对于独立的反转义,可以使用

span.classname[onclick="document.getElementById('id01').style.display='none'"]
  =="&times;"

记住:确保转义字符被双引号包围!


-1
<%= text_field_tag nil, nil, placeholder: "I'm looking for&hellip;".html_safe %>

或者提供更多有关您输入助手的信息


那不是SLIM,那是ERB。 - Papipo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接