KR / KO

What Is YAML and Can You Write Multiline Strings?

What Is YAML and Can You Write Multiline Strings?

YAML is short for “yet another markup language” or “YAML ain’t markup language,” the latter recursive acronym being used to emphasize that YAML is for data and not documents. YAML is a data serialization language used to write configuration files for deployment of infrastructure. For example, if you want to deploy a Docker application to Kubernetes, you would use a YAML file to configure the instance. YAML is human-readable, so it’s easy to create a file and read instructions from it during deployments.

If you’re new to YAML, it won’t be long before you realize you need a way to break up your strings into multiple lines, one of the most frequently asked YAML questions on the web. In this article, we’ll cover the two main approaches to YAML multiline strings, as well as some other frequently asked questions.

YAML and Whitespace: What to Know

YAML files use whitespaces and carriage returns, but the content is serialized when ingested into a specific resource. Developers creating a YAML file can format text using indents and spacing to make it easier to read. YAML treats whitespaces similarly to other languages, so you can have as many as needed to format a file.

How to Break Lines for YAML Multiline Strings

When ingesting YAML files, you might occasionally want to keep the line breaks in a multiline string. Take the following pseudocode as an example:

    key:

            item1

            item2

            item3

After every line item (e.g., item1, item2, and item3), you have a carriage return. If you leave the YAML syntax as is, the string becomes the following:

item1 item2 item3

This string format creates one long string without any carriage return formatting, so you might want to preserve the line breaks. Line breaks can be preserved with the literal operator. The literal operator is the pipe (“|”) character. To preserve line breaks, your YAML syntax should be changed to the following:

    key: |

            item1

            item2

            item3

Notice the pipe character after the “key” directive. When the application ingests the YAML file, it will convert to the following string:

item1\nitem2\nitem3

The “\n” character represents a new line. Printing this string to an application will display each item with an appended carriage return to preserve the formatting in the YAML instructions.

YAML Multiline Blocks vs. Long Strings

For long strings, you need to use multiline blocks to read a YAML file. For example, if you have a 250-word paragraph in a YAML file, you need to break the sentences into multiline blocks. You wouldn’t want to preserve the carriage returns except at the end of a paragraph. You can format a YAML file to use multiple lines for long strings and serialize the string to append spaces to each item instead of a new line character. 

To use a whitespace character instead of appending a new line, use the folded (“>”) operator:

    key: >

            item1

            item2

            item3

With the folded operator, YAML adds a space when it creates the string. The string looks like the following:

item1 item2 item3\n

YAML Multiline FAQs

How do you insert a line break in YAML?

Using the literal operator (“|”), any line breaks in a YAML string will be preserved. You create a line break using the carriage return keystroke in the same way that you create a line break in a standard document. The following example preserves the carriage return in a YAML string:

    key: |

            item1

            item2

            item3

Can YAML have blank lines?

Yes, a string represented as a block of text can also contain blank lines. The keep operator (“|+”)  preserves any blank empty lines along with the new line character. The following YAML is an example of a multiline string with two empty lines at the end of the block:

    key: |+

            item1

            item2

            item3

A YAML parser will display the following string from the above syntax:

item1\nitem2\nitem3\n\n

Does YAML care about whitespace?

A tab whitespace is an illegal character in YAML, so any indents must be used with a standard single space character. Tabs can be used in quoted strings, but they cannot be used to format a YAML file. Consistency in whitespace indents is important for readability of data.

Are spaces allowed in YAML?

Yes, you can have standard spaces throughout a YAML file. Spaces should be used for indents and not the tab character. Tab characters could cause errors during the parsing of a YAML file.

연락처
질문하기

퓨어스토리지 제품이나 인증 관련 질문이나 코멘트가 있으신가요?   저희가 도와드립니다.

데모 예약

라이브 데모를 예약하고 퓨어스토리지가 데이터를 어떻게 강력한 결과로 전환해주는지 직접 확인해 보세요. 

연락하기: +82 2 6001-3330

언론홍보팀:  pr@purestorage.com

 

퓨어스토리지코리아 주소

30F 아셈타워,

517 영동대로,

강남구, 서울

대한민국

korea@purestorage.com

닫기
지원하지 않는 브라우저입니다.

오래된 브라우저는 보안상 위험을 초래할 수 있습니다. 최상의 경험을 위해서는 다음과 같은 최신 브라우저로 업데이트하세요.