php yml文件,YAML文件结构简介

在看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.

Numbers12 #十进制

014 #八进制

0xC #十六进制

13.4 #浮点数

1.2e+24 #指数

.inf #无穷

Nullsnull

~

Booleanstrue

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 /)和标记引用(例如:!);

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

---

仅供个人理解参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值