2017-10-17 77 views
0

我在我的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

回答

0

經過不同的平臺,我終於找到解決方案,看代碼。

(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

這是一個黑客。這不是「正確」的方式。等一下,我會在一分鐘內給你一個正確的答案...... –

1

&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