如何在haml中添加空格

问题描述:

我在我的haml文件中有以下几行代码。如何在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: " " 

后有一个白色的空间,但该行没有工作。

+1

生成的HTML如何以及您的预期结果是什么? – Stefan

经过不同的平台,我终于找到解决方案,看代码。

(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

+0

这是一个黑客。这不是“正确”的方式。等一下,我会在一分钟内给你一个正确的答案...... –

&nbsp stands for non-breaking space空间,您可以使用简码(& NBSP)。

通常情况下,HTML会截断文本中的空格。如果你写10个空格,只会显示1个空格。 &nbsp强制出现的其中一种方式,但它很少是“正确”的方式。

一个有效的用例可能是:Mr.&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