HAML 中的多行字符串

Multiline string in HAML

因为我不想让任何一行的宽度超过 80 列,所以我想将最后一行分成两行。在行尾添加逗号按建议 here 工作,但随后逗号出现在字符串内。是否有使用 HAML 执行此操作的简洁明了的方法?

- if object.errors.any?
  %section.form_error_message
    %h4.form_error_message__title
      = "#{pluralize(object.errors.size, 'errors')} prevented this form from submitting. Please fix these errors and try again."

我希望所有内容都包含在 80 列中,就像它们位于此屏幕截图的左侧一样。

Haml has support for multiline sequences 这样使用 | 字符:

- if object.errors.any?
  %section.form_error_message
    %h4.form_error_message__title
      = "#{pluralize(object.errors.size, 'errors')} prevented this form from |
        submitting. Please fix these errors and try again."                  |

在这种情况下你真的不需要这个,你可以直接在纯文本中使用 interpolation,你不需要 =:

- if object.errors.any?
  %section.form_error_message
    %h4.form_error_message__title
      #{pluralize(object.errors.size, 'errors')} prevented this form from
      submitting. Please fix these errors and try again.

(此输出会略有不同,因为它将包含换行符。不过,您不应该在呈现的 HTML 中注意到这一点,除非您在 <pre> 标签内。 )

%div First line |
     and second line |

将呈现:

<div>First line and second line</div>

如果您甚至不需要标签:

First line |
and second line |

将产生:

First line and second line

您可以使用命令工具 haml,例如复制代码然后 pbpaste | haml 在 macOS 上,或者使用文件 cat file.haml | haml.