Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly have multi line yaml strings?

newlines on multiple lines does not seem to work out for me:

Something like:

  intro: |
    We are happy that you are interested in
    and  
    more

and + more needs to be on a newline but it fails.

  intro: |
    | We are happy that you are interested in
    | and  
    | more

or

  intro: |
    We are happy that you are interested in \n
    and  
    more <2 spaces >
    another one

All fail.

How to correctly have multiline in a yaml text block?

I use this in HAML view in rails app like

= t("mailer.beta_welcome.intro")

But no newlines are printed this way, do i need to output it differently with raw or something?

like image 395
Rubytastic Avatar asked Jan 12 '23 22:01

Rubytastic


2 Answers

Your first example works fine

foo.yml

intro: |
  We are happy that you are interested in
  and  
  more

foo.rb

require 'yaml'
puts YAML.load_file('foo.yml').inspect

Output

{"intro"=>"We are happy that you are interested in\nand  \nmore\n"}
like image 144
maček Avatar answered Jan 29 '23 19:01

maček


Late answer for Googlers:

It looks like you were trying to output it as HTML, which means it was indeed outputting the newlines if you were to inspect the page. HTML largely ignores whitespace, however, so your newlines and spaces were being converted into just a space by the HTML renderer.

According to the simple_format docs, simple_format applies a few simple formatting rules to text output in order to render it closer to what the plaintext output would be - significantly, it converts newlines to <br/> tags.

So your problem had nothing to do with YAML, which was performing as expected. It was actually because of how HTML works, which is also as expected. simple_format fixed it because it took your string from YAML with newlines and converted it to a string with <br/> tags so that the newlines actually showed up in the HTML, which is what you wanted in the first place.

like image 22
cincodenada Avatar answered Jan 29 '23 21:01

cincodenada