Indentation and separation are two main concepts when you are learning any programming language. This chapter talks about these two concepts related to YAML in detail.
YAML does not include any mandatory spaces. Further, there is no need to be consistent. The valid YAML indentation is shown below −
a: b: - c - d - e f: "ghi"
You should remember the following rules while working with indentation in YAML:Flow blocks must be intended with at least some spaces with surrounding current block level.
Flow content of YAML spans multiple lines. The beginning of flow content begins with { or [.
Block list items include same indentation as the surrounding block level because - is considered as a part of indentation.
Observe the following code that shows indentation with examples −
--- !clarkevans.com/^invoice invoice: 34843 date : 2001-01-23 bill-to: &id001 given : Chris family : Dumars address: lines: | 458 Walkman Dr. Suite #292 city : Royal Oak state : MI postal : 48046 ship-to: *id001 product: - sku : BL394D quantity : 4 description : Basketball price : 450.00 - sku : BL4438H quantity : 1 description : Super Hoop price : 2392.00 tax : 251.42 total: 4443.52 comments: > Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.
Strings are separated using double-quoted string. If you escape the newline characters in a given string, it is completely removed and translated into space value.
In this example we have focused listing of animals listed as an array structure with data type of string. Every new element is listed with a prefix of hyphen as mentioned as prefix.
- - Cat - Dog - Goldfish - - Python - Lion - Tiger
Another example to explain string representation in YAML is mentioned below.
errors: messages: already_confirmed: "was already confirmed, please try signing in" confirmation_period_expired: "needs to be confirmed within %{period}, please request a new one" expired: "has expired, please request a new one" not_found: "not found" not_locked: "was not locked" not_saved: one: "1 error prohibited this %{resource} from being saved:" other: "%{count} errors prohibited this %{resource} from being saved:"
This example refers to the set of error messages which a user can use just by mentioning the key aspect and to fetch the values accordingly. This pattern of YAML follows the structure of JSON which can be understood by user who is new to YAML.