我有一个用长语法编写的YAML文件
children:
-
type: section
subtype: false
title: Top-wrapper
attributes: { }
children:
-
type: section
subtype: false
title: Second-wrapper
attributes: { }
children:
-
type: section
subtype: false
title: Header
attributes: { }
children:
-
type: container
subtype: false
title: Untitled
attributes: { }
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children:
-
type: block
subtype: false
title: Untitled
attributes:
size: 100
children:
-
type: spacer
subtype: false
title: Spacer
attributes:
enabled: 1
children: { }
-
type: section
subtype: false
title: Navigation
attributes: { }
children:
-
type: container
subtype: false
title: Untitled
attributes: { }
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children:
-
type: block
subtype: false
title: Untitled
attributes:
size: 100
children:
-
type: spacer
subtype: false
title: Spacer
attributes:
enabled: 1
children: { }
-
type: section
subtype: false
title: Showcase
attributes: { }
children:
-
type: container
subtype: false
title: Untitled
attributes: { }
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children:
-
type: block
subtype: false
title: Untitled
attributes:
size: 100
children:
-
type: spacer
subtype: false
title: Spacer
attributes:
enabled: 1
children: { }
-
type: section
subtype: false
title: Feature
attributes: { }
children:
-
type: container
subtype: false
title: Untitled
attributes: { }
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children:
-
type: block
subtype: false
title: Untitled
attributes:
size: 100
children:
-
type: spacer
subtype: false
title: Spacer
attributes:
enabled: 1
children: { }
-
type: section
subtype: false
title: Main-content
attributes: { }
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children:
-
type: block
subtype: false
title: Untitled
attributes:
size: 20
children:
-
type: section
subtype: false
title: Sidebar-left
attributes: { }
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children:
-
type: block
subtype: false
title: Untitled
attributes:
size: 100
children:
-
type: position
subtype: false
title: Sidebar-left
attributes:
enabled: 1
key: sidebar-left
title: Sidebar-left
children: { }
-
type: block
subtype: false
title: Untitled
attributes:
size: 60
children:
-
type: section
subtype: false
title: Main
attributes: { }
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children:
-
type: block
subtype: false
title: Untitled
attributes:
size: 100
children:
-
type: pagecontent
subtype: false
title: Pagecontent
attributes:
enabled: 1
children: { }
-
type: block
subtype: false
title: Untitled
attributes:
size: 20
children:
-
type: section
subtype: false
title: Sidebar-right
attributes: { }
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children:
-
type: block
subtype: false
title: Untitled
attributes:
size: 100
children:
-
type: position
subtype: false
title: Sidebar-right
attributes:
enabled: 1
key: sidebar-right
title: Sidebar-right
children: { }
-
type: section
subtype: false
title: Subfeature
attributes: { }
children:
-
type: container
subtype: false
title: Untitled
attributes: { }
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children:
-
type: block
subtype: false
title: Untitled
attributes:
size: 100
children:
-
type: spacer
subtype: false
title: Spacer
attributes:
enabled: 1
children: { }
-
type: section
subtype: false
title: Footer
attributes: { }
children:
-
type: container
subtype: false
title: Untitled
attributes: { }
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children:
-
type: block
subtype: false
title: Untitled
attributes:
size: 100
children:
-
type: spacer
subtype: false
title: Spacer
attributes:
enabled: 1
children: { }
-
type: offcanvas
subtype: false
title: Offcanvas
attributes:
name: 'Offcanvas Section'
children: { }
-
type: atoms
subtype: false
title: Atoms
attributes:
name: 'Atoms Section'
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children: { }
我想将其转换为使用类似于此
的简写语法layout:
1:
- top-wrapper 100:
header:
container:
- spacer
navigation:
container:
- [particle-logo 20, particle-menu 80]
showcase:
container:
- spacer
feature:
container:
- spacer
main:
container:
- system-messages
- pagecontent
footer:
container:
- spacer
debug:
container:
- spacer
offcanvas:
- particle-mobile-menu
我知道YAML有关于使用速记语法的一些规则,我该如何使用它?
答案 0 :(得分:1)
YAML规范中指定的缩短YAML的唯一方法是使用anchor s和对这些锚点的引用(称为别名)。任何其他缩短都必须在程序中完成,该程序使用YAML通过语义解释特定标量生成的数据。
内置锚机制通常用于让YAML文档共享原始数据,例如如果在映射和序列的层次结构中的两个位置引用了一个映射。这允许您将输入重写为:
children:
-
type: section
subtype: false
title: Top-wrapper
attributes: { }
children:
-
type: section
subtype: false
title: Second-wrapper
attributes: { }
children:
-
type: section
subtype: false
title: Header
attributes: { }
children: &cont00 # <- define anchor for the sequence node
-
type: container
subtype: false
title: Untitled
attributes: { }
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children:
-
type: block
subtype: false
title: Untitled
attributes:
size: 100
children:
-
type: spacer
subtype: false
title: Spacer
attributes:
enabled: 1
children: { }
-
type: section
subtype: false
title: Navigation
attributes: { }
children: *cont00
-
type: section
subtype: false
title: Showcase
attributes: { }
children: *cont00 # <- use the achored node
-
type: section
subtype: false
title: Feature
attributes: { }
children: *cont00 # <- use the anchored node
-
type: section
subtype: false
title: Main-content
attributes: { }
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children:
-
type: block
subtype: false
title: Untitled
attributes:
size: 20
children:
-
type: section
subtype: false
title: Sidebar-left
attributes: { }
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children:
-
type: block
subtype: false
title: Untitled
attributes:
size: 100
children:
-
type: position
subtype: false
title: Sidebar-left
attributes:
enabled: 1
key: sidebar-left
title: Sidebar-left
children: { }
-
type: block
subtype: false
title: Untitled
attributes:
size: 60
children:
-
type: section
subtype: false
title: Main
attributes: { }
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children:
-
type: block
subtype: false
title: Untitled
attributes:
size: 100
children:
-
type: pagecontent
subtype: false
title: Pagecontent
attributes:
enabled: 1
children: { }
-
type: block
subtype: false
title: Untitled
attributes:
size: 20
children:
-
type: section
subtype: false
title: Sidebar-right
attributes: { }
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children:
-
type: block
subtype: false
title: Untitled
attributes:
size: 100
children:
-
type: position
subtype: false
title: Sidebar-right
attributes:
enabled: 1
key: sidebar-right
title: Sidebar-right
children: { }
-
type: section
subtype: false
title: Subfeature
attributes: { }
children: *cont00
-
type: section
subtype: false
title: Footer
attributes: { }
children: *cont00
-
type: offcanvas
subtype: false
title: Offcanvas
attributes:
name: 'Offcanvas Section'
children: { }
-
type: atoms
subtype: false
title: Atoms
attributes:
name: 'Atoms Section'
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children: { }
children:
-
type: section
subtype: false
title: Top-wrapper
attributes: { }
children:
-
type: section
subtype: false
title: Second-wrapper
attributes: { }
children:
-
type: section
subtype: false
title: Header
attributes: { }
children: *cont00
-
type: section
subtype: false
title: Navigation
attributes: { }
children: *cont00
-
type: section
subtype: false
title: Showcase
attributes: { }
children: *cont00
-
type: section
subtype: false
title: Feature
attributes: { }
children: *cont00
-
type: section
subtype: false
title: Main-content
attributes: { }
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children:
-
type: block
subtype: false
title: Untitled
attributes:
size: 20
children:
-
type: section
subtype: false
title: Sidebar-left
attributes: { }
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children:
-
type: block
subtype: false
title: Untitled
attributes:
size: 100
children:
-
type: position
subtype: false
title: Sidebar-left
attributes:
enabled: 1
key: sidebar-left
title: Sidebar-left
children: { }
-
type: block
subtype: false
title: Untitled
attributes:
size: 60
children:
-
type: section
subtype: false
title: Main
attributes: { }
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children:
-
type: block
subtype: false
title: Untitled
attributes:
size: 100
children:
-
type: pagecontent
subtype: false
title: Pagecontent
attributes:
enabled: 1
children: { }
-
type: block
subtype: false
title: Untitled
attributes:
size: 20
children:
-
type: section
subtype: false
title: Sidebar-right
attributes: { }
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children:
-
type: block
subtype: false
title: Untitled
attributes:
size: 100
children:
-
type: position
subtype: false
title: Sidebar-right
attributes:
enabled: 1
key: sidebar-right
title: Sidebar-right
children: { }
-
type: section
subtype: false
title: Subfeature
attributes: { }
children: *cont00
-
type: section
subtype: false
title: Footer
attributes: { }
children: *cont00
-
type: offcanvas
subtype: false
title: Offcanvas
attributes:
name: 'Offcanvas Section'
children: { }
-
type: atoms
subtype: false
title: Atoms
attributes:
name: 'Atoms Section'
children:
-
type: grid
subtype: false
title: Untitled
attributes: { }
children: { }
即。您使用&
+“唯一ID”标记节点(以及下面的所有内容)
并使用*
+“唯一ID”
这不是字符串的重写机制,例如C中的宏,因此没有参数化:节点结构必须完全匹配才能使其工作。在读取这样的YAML文件时,最终应该在两个位置使用一个对象。
如果你想要参数化,你需要创建一些自己加载(和写入)的机制,这可能会进一步减少文件的长度,但YAML无法扩展,因为只有你的程序才会知道如何这样做。