當學習任何編程語言時,縮進和分離是兩個主要概念。本章詳細討論了與YAML相關(guān)的這兩個概念。
YAML不包括任何強制性空格。此外,沒有必要保持一致。有效的YAML縮進如下所示
a:
b:
- c
- d
- e
f:
"ghi"
預期塊的示例請注意以下代碼,其中顯示了縮進示例
--- !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.
字符串使用雙引號字符串分隔。如果轉(zhuǎn)義給定字符串中的換行符,則會將其完全刪除并轉(zhuǎn)換為空格值。
示例在這個例子中,將動物列表列為數(shù)據(jù)類型為string的數(shù)組結(jié)構(gòu)。列出的每個新元素都帶有連字符前綴,如前綴所述。
-
- Cat
- Dog
- Goldfish
-
- Python
- Lion
- Tiger
另一個解釋YAML中字符串表示的例子如下所述。
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:"
此示例引用了一組錯誤消息,用戶只需提及關(guān)鍵方面并相應(yīng)地獲取值即可使用。 這種YAML模式遵循JSON的結(jié)構(gòu),YAML的新用戶可以理解。
更多建議: