【问题标题】:How to add white spaces in haml如何在haml中添加空格
【发布时间】:2017-10-17 07:48:25
【问题描述】:

我的 haml 文件中有以下几行。

(Requested:
                            %strong<>
                              = comment.commentable.requested_check_in.present? ? comment.commentable.requested_check_in.strftime("%m/%d/%Y") : ""
                              ="-"
                              = comment.commentable.requested_check_out.present? ? comment.commentable.requested_check_out.strftime("%m/%d/%Y") : ""
                            , Actual:
                            %strong<>
                              = comment.commentable.actual_check_in.present? ? comment.commentable.actual_check_in.strftime("%m/%d/%Y") : ""
                              ="-"
                              = comment.commentable.actual_check_out.present? ? comment.commentable.actual_check_out.strftime("%m/%d/%Y") : ""
                            ).

我想要的是后面有一个空格

(Requested: " "

但是这条线不起作用。

【问题讨论】:

  • 生成的 HTML 是什么样子的,您的预期结果是什么?

标签: ruby-on-rails ruby haml


【解决方案1】:

在经历了不同的平台后,我终于找到了解决方案,见代码。

(Requested:&nbsp
                            %strong<>
                              = comment.commentable.requested_check_in.present? ? comment.commentable.requested_check_in.strftime("%m/%d/%Y") : ""
                              &nbsp-&nbsp
                              = comment.commentable.requested_check_out.present? ? comment.commentable.requested_check_out.strftime("%m/%d/%Y") : ""
                            , Actual:&nbsp
                            %strong<>
                              = comment.commentable.actual_check_in.present? ? comment.commentable.actual_check_in.strftime("%m/%d/%Y") : ""
                              &nbsp-&nbsp
                              = comment.commentable.actual_check_out.present? ? comment.commentable.actual_check_out.strftime("%m/%d/%Y") : ""
                            ).

你可以在haml中任何你想要空间的地方使用简码(&nbsp)

【讨论】:

  • 这是一个黑客。这不是“正确”的做法。等一下,我会在一分钟内给你一个正确的答案......
  • @TomLord 我听说过的最长的一分钟。
  • @jugglervr 我在上面发布了一个答案,3 年前! (但好吧,我确实花了 30 分钟,而不是 1 分钟。)
【解决方案2】:

&amp;nbsp stands for non-breaking space.

通常,HTML 会截断文本中的空格。如果你写 10 个空格,只会显示 1 个。 &amp;nbsp 是一种强制显示额外空格的方法,但它很少是“正确”的方法。

一个有效的用例可能是:Mr.&amp;nbspUsman - 强制这两个词一起出现在同一行。

您需要做的就是将文本用引号括起来:

= '(Requested: '
%strong= comment.commentable.requested_check_in&.strftime("%m/%d/%Y")
- if comment.commentable.requested_check_out
  = ' - '
  %strong= comment.commentable.requested_check_out.strftime("%m/%d/%Y")
= ')'
= '(Actual: '
%strong= comment.commentable.actual_check_in&.strftime("%m/%d/%Y")
- if comment.commentable.actual_check_out
  = ' - '
  %strong= comment.commentable.actual_check_out.strftime("%m/%d/%Y")
= ')'

然而,这段代码非常混乱。将如此复杂的逻辑放在视图中是不可取的。我建议将此逻辑移到辅助方法中;也许考虑使用draper 库?您重构的视图代码最终可能看起来像这样:

!= "(Requested: <strong>#{comment.commentable.requested_check_out_range}</strong>)"
!= "(Actual: <strong>#{comment.commentable.actual_check_out_range}</strong>)"

...将条件逻辑移至./app/decorators/*.rb

如果您确实需要使用 多个 非截断空白来格式化文本,那么您通常应该使用 white-space CSS property

【讨论】:

    【解决方案3】:

    如果您需要在 HAML 上留一个空格,请使用以下作弊码:

      = parseFloat(elem.value) > 1.00 ? 'X' : "\n\b"
    

    希望对你有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-23
      • 1970-01-01
      • 2018-06-29
      • 2017-04-11
      • 1970-01-01
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多