2016-01-19 15 views
2

因爲我不想讓我的任何行超過80列,所以我想將最後一行分成兩行。在行尾添加逗號的方式如建議here那樣工作,但逗號出現在字符串內部。用HAML做這件事是否有一個很好和乾淨的方法?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列,就像它們在這個截圖的左邊。

enter image description here

+1

如果這些是您稍後想要翻譯的文本,那麼您現在可以切換到使用I18n。那麼你沒有這樣的問題。 – Raffael

+0

或者,您可以使用':ruby'塊將視圖模板中的字符串保存在視圖模板的局部變量中。 – Raffael

+0

謝謝,你說得對,我現在不妨看看國際米蘭。我不認爲我的第一個Rails應用程序會永遠完成,因爲我不斷遇到另一個寶石納入或另一個最佳做法(不是這是一件壞事!) –

回答

10

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>標籤)

+0

甜!我不知道你可以在純文本內插入Ruby代碼。 –

0
%div First line | 
    and second line | 

將呈現:

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

如果你甚至不想標籤:

First line | 
and second line | 

會產生:

First line and second line 

你可以使用命令工具haml,例如複製macOS上的代碼pbpaste | haml或文件cat file.haml | haml