如何在 HAML 中将 link 放入文本字​​符串中?

How can I put a link inside of a text string in HAML?

这应该比现在更容易。我只想在 HTML 段落元素中放置一个 link。

%p{class: "answer"}="Please upload your data to this portal in text (tab-separated) format. Download our template #{raw(link_to 'here', '/templates/upload_template.xlsx')} for sample data and a description of each column."

Rails正在对标签信息进行编码。我不想对标签进行编码。我希望它们成为标签。

你可以在任何块中使用多行,为了解决你的问题,我们将有这样的东西:

%p{class: "answer"}
  Please upload your data to this portal in text (tab-separated) format. Download our template 
  = link_to 'here', '/templates/upload_template.xlsx'
  for sample data and a description of each column."

您可以使用 interpolation directly in Haml,在这种情况下这样做似乎可以解决问题。

所以不要这样做:

%p= "Text with #{something_interpolated} in it."

你可以做到

%p Text with #{something_interpolated} in it.

即您不需要 = 或引号,因为您只是添加了一个字符串。您也不需要使用 raw

(另外,请注意,您可以执行 %p.answer 来设置 class 属性,如果该值不是动态设置的,这可能会更清晰。)


为什么这里要转义是另一回事。我原以为这两种情况(%p= "#{foo}"%p #{foo})的行为方式相同。然而,经过一些研究,这种行为似乎与 Rails 与 Erb 的行为相匹配。