温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

YAML文件结构简介

发布时间:2020-07-06 20:29:12 来源:网络 阅读:880 作者:ADUJ 栏目:web开发

在看drupal8,发现其模块和主题的定义使用的是YAML文件。drupal8使用的Symfony框架中YAML文件结构,故翻译整理学习下,有理解不准确的地方请多多指教。

String

一般字符串可以使用单引号,双引号或者不使用引号

A string in YAML
'A singled-quoted string in YAML'
"A double-quoted string in YAML"

当使用单引号字符串时,其内容中的任何单引号必须加倍才能转义:

'A single quote '' inside a single-quoted string'

包含以下任何字符都需要引号,可以使用单引号或双引号,但使用单引号更方便

:, {, }, [, ], ,, &, *, #, ?, |, -, <, >, =, !, %, @, `

双引号方式提供了以一种表示任意字符串的方法,使用\来转义字符和序列

\0, \x01, \x02, \x03, \x04, \x05, \x06, \a, \b, \t, \n, \v, \f, \r, \x0e, \x0f, \x10, \x11,  \x12, \x13, \x14, \x15, \x16, \x17, \x18, \x19, \x1a, \e, \x1c, \x1d, \x1e, \x1f, \N, \_, \L, \P

"A double-quoted string in YAML\n"

注:测试发现,当字符串中包含上面字符时,使用单引号也可以转义的

最后还有其他一些情况,不管你使用单引号还是双引号

  • true or false ( 否则就是布尔值 )  

  • null or ~ (否则认为是null)

  • 当看起来像整数如2,14;浮点数如2.6,14.9;指数如12e7等,否则被视为一个数值

  • 当看起来像一个日期如2014-12-31,否则会自动转为一个Unix时间戳

'true'
'null'
'2.14'
'2014-12-31'

当一个字符串中包含换行符时,可以使用由(|)管道符来指示该字符串将跨越多行。在文字中,换行符被保存下来

|
  \/ /| |\/| |
  / / | |  | |__

或者字符串可以使用折叠样式编写,用>表示,其中每行换行符被空格替换

>  
  This is a very long sentence
  that spans several lines in the YAML
  but which will be rendered as a string
  without carriage returns.

Numbers

12 #十进制
014 #八进制
0xC #十六进制
13.4 #浮点数
1.2e+24 #指数
.inf #无穷

Nulls

null
~

Booleans

true
false

Dates

#简单日期
2002-12-14

Collections

- PHP
- Perl
- Python

对应PHP为

array('PHP','perl','Python')
PHP:    5.2
MySQL:  5.1
Apache: 2.2.20

对应PHP为

array('PHP' => 5.2, 'MySQL' => 5.1, 'Apache' => '2.2.20');
'symfony 1.0':
  PHP:    5.0
  Propel: 1.2
'symfony 1.2':
  PHP:    5.2
  Propel: 1.3

对应PHP为

array(
    'symfony 1.0' => array(
        'PHP'    => 5.0,
        'Propel' => 1.2,
    ),
    'symfony 1.2' => array(
        'PHP'    => 5.2,
        'Propel' => 1.3,
    ),
);

在YAML文件中使用缩进时需要记住一件重要的事情:缩进必须由一个或多个空格完成,但从不使用制表符。

也可以根据需要嵌套序列和映射

'Chapter 1':
  - Introduction
  - Event Types
'Chapter 2':
  - Introduction
  - Helpers

也可使用明显的指示符而不是缩进

[PHP, Perl, Python] 
{ PHP: 5.2, MySQL: 5.1, Apache: 2.2.20 }
'Chapter 1': [Introduction, Event Types]
'Chapter 2': [Introduction, Helpers]
'symfony 1.0': { PHP: 5.0, Propel: 1.2 }
'symfony 1.2': { PHP: 5.2, Propel: 1.3 }

注释

#comment on a line

Explicit Typing

YAML规范定义了一些标签来显式地设置任何数据类型:

data:
  # this value is parsed as a string (it's not transformed into a DateTime)
  start_date: !!str 2002-12-14

  # this value is parsed as a float number (it will be 3.0 instead of 3)
  price: !!float 3

  # this value is parsed as binary data encoded in base64
  picture: !!binary |
  R0lGODlhDAAMAIQAAP//9/X
  17unp5WZmZgAAAOfn515eXv
  Pz7Y6OjuDg4J+fn5OTk6enp

Unsupported YAML Features

Symfony Yaml组件不支持以下YAML特性:

  • Multi-documents (--- and ... markers); 多文档(---和...标记);

  • Complex mapping keys and complex values starting with ?;以?开头的复杂映射键和复数值;

  • Tagged values as keys;标记为键的值;

  • 下面标签和类型: !!set, !!omap, !!pairs, !!set, !!seq, !!bool, !!int, !!merge, !!null, !!timestamp, !!value, !!yaml;

  • 标记(TAG指令;例如:%TAG!标记:example.com,2000:app /)和标记引用(例如:!<tag:example.com,2000:app / foo>);

  • 映射元素使用类似序列的语法(例如:{foo,bar};改为使用{foo:〜,bar:〜})


原文地址:http://symfony.com/doc/current/components/yaml/yaml_format.html

验证地址:https://codebeautify.org/yaml-to-json-xml-csv


---

仅供个人理解参考


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI