56

I'm trying to implement a service catalog in Jekyll, in which each of 20 or 30 pages will contain a 7x2 table. The left column will hold labels, e.g. Overview, Available To, etc, while the right column will hold between one line and several paragraphs of text. I was hoping to characterize the right column with Liquid variables, e.g. {overview}, {availableTo}

I've noticed that the YAML seems to be very picky about line breaks, and accordingly I've had to input these paragraphs and their markup on one line which can go on for several screen-widths. This is a problem because it's annoying, and also because I'd like these front-matters to be editable by technical but non-webdev users. Is there a way to have the front matter tolerate breaks?

Alternatively, is there a way that I could populate this table with the {content} section, without having to recode the table into it each time?

patrickjmc
  • 569
  • 1
  • 4
  • 3
  • possible duplicate of [In YAML, how do I break a string over multiple lines?](http://stackoverflow.com/questions/3790454/in-yaml-how-do-i-break-a-string-over-multiple-lines) – akostadinov Jul 01 '15 at 09:24

1 Answers1

91

Yaml syntax for multi-line strings is this one:

body: |
  This is a multi-line string.
  "special" metacharacters may
  appear here. The extent of this string is
  indicated by indentation. 

Notice that the first line must be an space followed by the | character and a new line. Then you must indent the text one level more than its parent.

Consequently, you can create one item this way:

item1:
  overview: |
    overview text
    more overview text
  available_to: 2012-01-01
  foo: |
    foo text
    more foo text

It seems to me that you also want to arrange your items in order. You can employ a yaml list for that:

catalog:
  - id: item 1
    overview: |
      overview text
      more overview text
    available_to: 2012-01-01
    foo: |
      foo text
      more foo text
    ...
  - id: item2
    overview: <similar to above>
starball
  • 20,030
  • 7
  • 43
  • 238
kikito
  • 51,734
  • 32
  • 149
  • 189
  • 1
    Thanks, this also helped me solving the problem of how to add quote characters like " to the YAML front matter! – Martin Apr 20 '12 at 13:18
  • 6
    FWIW, there are [very many](http://stackoverflow.com/questions/3790454/in-yaml-how-do-i-break-a-string-over-multiple-lines/21699210#21699210) different multi-line string formats supported by YAML. In many cases, `>` is better than `|` because it doesn't insert newlines at the end of each line. – Steve Bennett Oct 04 '16 at 22:13
  • @Philll_t you would add the [markdownify filter](https://jekyllrb.com/docs/templates/) in the output: `{{ foo | mardownify }}`. I found that in order to achieve linebreaks, you also need to add extra linebreaks to the frontmatter. [here](https://stackoverflow.com/questions/27701911/jekyll-cannot-render-yaml-front-matter-field-as-markdown-correctly) is how that would all work – Edward Mar 29 '18 at 19:26
  • May also need `newline_to_br` filter, like `{{ x | markdownify | newline_to_br }}` – gingerCodeNinja Apr 18 '22 at 20:11