4

Because I don't want any of my lines to be wider than 80 columns, I want to split the last line into two lines. Adding a comma at the end of the line works as suggested here but then the comma appears inside the string. Is there a nice and clean way of doing this with 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."

I want it all to be tucked inside 80 columns like they are on the left of this screenshot.

enter image description here

Community
  • 1
  • 1
Sajad Torkamani
  • 544
  • 1
  • 7
  • 18
  • 1
    If these are Texts that you might want to translate later on, you might as well switch to using I18n now. Then you don't have such issues. – Raffael Jan 19 '16 at 16:25
  • Alternatively, you might save your String in a local variable in the view template using a `:ruby` block. – Raffael Jan 19 '16 at 16:26
  • Thanks, you're right in that I might as well look at I18n now. I don't think my first rails application will ever finish as I keep coming across another gem to incorporate or another best practice (not that this a bad thing!) – Sajad Torkamani Jan 19 '16 at 16:33

2 Answers2

19

Haml has support for multiline sequences like this using the | character:

- 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."                  |

In this case you don’t really need this though, you can use interpolation directly in plain text, you don’t need the =:

- 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.

(The output of this will be slightly different, as it will include the newline. You should’t notice that in the rendered HTML though, unless you are e.g. inside <pre> tags.)

matt
  • 78,533
  • 8
  • 163
  • 197
1
%div First line |
     and second line |

Will render:

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

If you don't even want a tag:

First line |
and second line |

Will produce:

First line and second line

You can use the command-tool haml, for instance copy the code then pbpaste | haml on macOS, or with a file cat file.haml | haml.

Dorian
  • 22,759
  • 8
  • 120
  • 116