symfony入门学习资料之九:YAML格式介绍
由于EDUSOHO的项目原因,中断了对symfony框架的继续学习,今天回顾了前面的总结,总结了八节。从建立简单的页面、控制器、路由、模板等,甚至包括前面的安装等介绍一系列文章。结合中间插入的edusoho等系列文章的研究,认为学习symfony的框架,还需要细化学习yaml格式和twig的格式文件。这一节开始介绍yaml格式。
YAML(发音 /ˈjæməl/ )是一种专门用户描述数据的简单语言,支持复杂的可嵌套的数据结构。其基本的语法规则是:
- 大小写敏感
- 使用缩进表示层级关系
- 缩进时不允许使用Tab键,只允许使用空格。
- 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
一、标量定义
1.1 字符串
字符串可以用单引号或者双引号进行包裹起来,在有些情况下也可以不用引号。
A string in YAML
'A singled-quoted string in YAML'
"A double-quoted string in YAML"
如果字符串的首部或者尾部有空白字符,那么必须使用引号,否则在解析文件的时候会将首部和尾部的空白字符移除。
如果在字符串中包含下面的字符,必须要使用引号,如果你使用单引号的话可以避免转义,如果使用双引号那么在下面的字符前面必须使用转义字符“\”:
:, {, }, [, ], ,, &, *, #, ?, |, -, <, >, =, !, %, @, `
如果在字符串中包含了如下的控制字符,必须要使用双引号
\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
此外,在下面这几种情况下也必须使用引号进行包裹:
当字符串是 true 或者 false (否则的话,会被认为是一个布尔值);
当字符串是 null或者 ~ (否则的话,会被认为是一个null);
当字符串像一个数字,比如整数(2,4)、浮点数(2.6,12.3)、科学计数(12e7)等等可以被看作是数字的字符串;
当字符串看起来像一个日期格式(2014-12-31);
如果想通过多行编写的方式来定义一个字符串,那么可以使用“>”,换行符会被替换为1个空格字符。
>
This is a very long sentence
that spans several lines in the YAML
but which will be rendered as a string
without carriage returns.
每行前面使用2个空格字符表明继续使用前一种定义语法,并且不会出现在最终的字符串中。
1.2 数字
# 整数
12
# 八进制
014
# 十六进制
0x0C
# 浮点数
13.4
# 科学计数法
1.2e+34
# 无穷大
.inf
1.3 空值
null或者~
1.4 布尔值
true和false
1.5 日期
使用ISO-8601标准
2001-12-14t21:59:43.10-05:00
# 简单的日期
2002-12-14
二、集合
使用间断字符“-”来定义元素序列
- 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');
YAML中使用缩进或者更多的空格字符来表示嵌套的数据结构,比如下面:
'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,
),
);
注意:缩进必须使用1个或者更多的空格字符,而不能使用制表符。你也可以使用下面这种嵌套方式:
'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 }
三、注释
YAML采用行注释,使用“#”
# 注释一行
"symfony 1.0": { PHP: 5.0, Propel: 1.2 } # 在行尾进行注释
"symfony 1.2": { PHP: 5.2, Propel: 1.3 }
四、关于yml格式类型文件及YAML的一些资源
在drupal中用了大量yml扩展名的配置文件,这是因为symfony采用了这样的配置文件,yml文件扩展名是YAML的缩写,YAML于2001年出现,是一种数据描述语言,类似xml,但非常简洁,在2009年发布了1.2版本,在php中有php-yaml,syck,spyc进行解析
YAML的官网是:http://www.yaml.org/
以下是其官网2016年4月6日的首页内容:
%YAML 1.2
---
YAML: YAML Ain't Markup Language
What It Is: YAML is a human friendly data serialization
standard for all programming languages.
YAML Resources:
YAML 1.2 (3rd Edition): http://yaml.org/spec/1.2/spec.html
YAML 1.1 (2nd Edition): http://yaml.org/spec/1.1/
YAML 1.0 (1st Edition): http://yaml.org/spec/1.0/
YAML Issues Page: https://github.com/yaml/yaml/issues
YAML Mailing List: yaml-core@lists.sourceforge.net
YAML IRC Channel: "#yaml on irc.freenode.net"
YAML Cookbook (Ruby): http://yaml4r.sourceforge.net/cookbook/ (local)
YAML Reference Parser: http://ben-kiki.org/ypaste/
Projects:
C/C++ Libraries:
- libyaml # "C" Fast YAML 1.1
- Syck # (dated) "C" YAML 1.0
- yaml-cpp # C++ YAML 1.2 implementation
Ruby:
- psych # libyaml wrapper (in Ruby core for 1.9.2)
- RbYaml # YAML 1.1 (PyYaml Port)
- yaml4r # YAML 1.0, standard library syck binding
Python:
- PyYaml # YAML 1.1, pure python and libyaml binding
- PySyck # YAML 1.0, syck binding
Java:
- JvYaml # Java port of RbYaml
- SnakeYAML # Java 5 / YAML 1.1
- YamlBeans # To/from JavaBeans
- JYaml # Original Java Implementation
Perl Modules:
- YAML # Pure Perl YAML Module
- YAML::XS # Binding to libyaml
- YAML::Syck # Binding to libsyck
- YAML::Tiny # A small YAML subset module
- PlYaml # Perl port of PyYaml
C#/.NET:
- YamlDotNet # YAML 1.1 library with serialization support
- yaml-net # YAML 1.1 library
- yatools.net # (in-progress) YAML 1.1 implementation
Golang:
- Go-yaml # YAML support for the Go language.
- Go-gypsy # Simplified YAML parser written in Go.
PHP:
- php-yaml # libyaml bindings (YAML 1.1)
- syck # syck bindings (YAML 1.0)
- spyc # yaml loader/dumper (YAML 1.?)
OCaml:
- ocaml-syck # YAML 1.0 via syck bindings
Javascript:
- JS-YAML # Native PyYAML port to JavaScript.
- JS-YAML Online# Browserified JS-YAML demo, to play with YAML in your browser.
Actionscript:
- as3yaml # port of JvYAML (1.1)
Haskell:
- YamlReference # Haskell 1.2 reference parser
Dart:
- yaml # YAML package for Dart
Others:
- yamlvim (src) # YAML dumper/emitter in pure vimscript
Related Projects:
- Rx # Multi-Language Schemata Tool for JSON/YAML
- Kwalify # Ruby Schemata Tool for JSON/YAML
- yaml_vim # vim syntax files for YAML
- yatools.net # Visual Studio editor for YAML
- JSON # Official JSON Website
- Pygments # Python language Syntax Colorizer /w YAML support
News:
- 20-NOV-2011 -- JS-YAML, a JavaScript YAML parser by Alexey Zapparov and Vitaly Puzrin.
- 18-AUG-2010 -- Ruby 1.9.2 includes psych, a libyaml wrapper by Aaron Patterson.
- 17-AUG-2010 -- vimscript parser/emitter was created by Nikolay Pavlov.
- 01-OCT-2009 -- YAML 1.2 (3rd Edition) was patched.
- 21-JUL-2009 -- YAML 1.2 (3rd Edition) was released.
- 28-APR-2009 -- A new version of SnakeYAML was released.
- 01-APR-2009 -- The YAML 1.2 spec was planned to be finalized by the end of the month.
- 07-JAN-2009 -- Andrey Somov releases SnakeYAML, a 1.1 YAML Parser
- 03-JAN-2009 -- Burt Harris announced YAML for .NET and editor for Visual Studio
- 02-DEC-2008 -- Jesse Beder released YAML for C++
- 11-MAY-2008 -- Oren Ben-Kiki has released a new YAML 1.2 spec draft
- 29-NOV-2007 -- Alexey Zakhlestin has updated his Syck (YAML 1.0) binding for PHP
- 23-NOV-2007 -- Derek Wischusen has release Action Script 3 YAML 1.1
- 01-AUG-2006 -- Kirill Simonov has released libyaml, a parser and emitter in "C"
- 06-JUN-2006 -- Ola Bini is at it again, this time with a Java implementation
- 03-JUN-2006 -- Christophe Lambrechts and Jonathan Slenders announced a .NET parser
- 07-MAY-2006 -- Ola Bini released a pure-ruby YAML 1.1 parser and emitter
- 12-APR-2006 -- Kirill's YAML 1.1 parser for Python is now at PyYaml
- 05-FEB-2006 -- Spyc YAML for PHP is now at version 0.3
- 17-DEC-2005 -- Makoto Kuwata has released Kwalify 0.5, YAML/JSON schema validator
- 14-DEC-2005 -- Toby Ho has released Jyaml, a Java library for YAML based on Rolf Veen's work
- 30-AUG-2005 -- Kirill Simonov has produce a wonderful Python binding for Syck
- 08-APR-2005 -- As it turns out, YAML is a superset of the JSON serialization language
- 18-MAY-2005 -- Why has released version 0.55 of Syck
- 28-DEC-2004 -- Announcing YAML 1.1 Working Draft
- 01-OCT-2004 -- YAML for Cocoa was released by Will Thimbley
- 08-FEB-2004 -- Slaven Rezic announced a new version of his Javascript binding
- 29-JAN-2004 -- Ingy, Oren, and Clark spent 4 days hacking on the spec in Portland.
- 10-OCT-2003 -- The Syck implementation with bindings for Ruby, Python,
and PHP is now at version .41
- 26-APR-2003 -- Mike Orr has taken over the Pure Python development.
- 26-APR-2003 -- Brian Ingerson has created a FIT platform for Wiki-like testing.
- 24-JAN-2003 -- Updates to specification.
- 25-JUL-2002 -- Both the Ruby and Python parsers have made significant progress.
There is an article about YAML by Kendall Grant Clark at xml.com.
There is also a draft XML binding.
- 02-JUL-2002 -- Brian Ingerson will be giving a 45 minute presentation on YAML at the
O'Reilly Open Source Conference in San Diego on July 24th 2002.
- 01-FEB-2002 -- Brian's Perl implementation YAML.pm, has been updated with new documentation.
Included in this release is YSH, a test shell for learning how YAML works.
- 03-JAN-2002 -- YAML(tm) starts the new year with a new name YAML Ain't Markup Language.
- 17-MAY-2001 -- YAML now has a mailing list at SourceForge.
- 15-MAY-2001 -- YAML is started with a first pass specification.
# Maintained by Clark C. Evans
...