如何在 Haml 中使用 %p 和附加行

How to use %p with additional lines in Haml

我收到以下错误:

Illegal nesting: content can't be both given on the same line as %p and nested within it.

这是我的代码:

- @docs.each do |doc|
%h2= link_to doc.title, doc
%p= time_ago_in_words(doc.created_at)
%p= truncate(doc.content, length:50)

    = link_to "Create Doc", new_doc_path

当您在与 Haml 中的标签相同的行中提供内容时,您说的是 "The content of this tag will only be whatever is in this line, and close the tag immediately"。

所以

%p hello

变成

<p>hello</p>

在标签有多行的情况下,使用换行符和缩进来指示该块的内容:

%p
  Hello
  World

这导致:

<p>Hello\nWorld</p>

您正试图在此处同时使用它们。您正在为短格式标签提供内容,但也进行了缩进,就好像您打算继续向 p 标签添加内容一样。

要解决此问题,您需要使用多行语法(如果您有意的话),或者从 link_to:

中删除缩进
- @docs.each do |doc|
  %h2= link_to doc.title, doc
  %p= time_ago_in_words(doc.created_at)
  %p
    = truncate(doc.content, length:50)
    = link_to "Create Doc", new_doc_path