@@ -21,6 +21,11 @@ as INI files.
21
21
The Symfony2 YAML Component implements the YAML 1.2 version of the
22
22
specification.
23
23
24
+ .. tip ::
25
+
26
+ Learn more about the Yaml component in the
27
+ :doc: `/components/yaml/yaml_format ` article.
28
+
24
29
Installation
25
30
------------
26
31
@@ -207,272 +212,5 @@ representation to the inline one:
207
212
foo : bar
208
213
bar : baz
209
214
210
- The YAML Format
211
- ---------------
212
-
213
- According to the official `YAML `_ website, YAML is "a human friendly data
214
- serialization standard for all programming languages".
215
-
216
- Even if the YAML format can describe complex nested data structure, this
217
- chapter only describes the minimum set of features needed to use YAML as a
218
- configuration file format.
219
-
220
- YAML is a simple language that describes data. As PHP, it has a syntax for
221
- simple types like strings, booleans, floats, or integers. But unlike PHP, it
222
- makes a difference between arrays (sequences) and hashes (mappings).
223
-
224
- Scalars
225
- ~~~~~~~
226
-
227
- The syntax for scalars is similar to the PHP syntax.
228
-
229
- Strings
230
- .......
231
-
232
- .. code-block :: yaml
233
-
234
- A string in YAML
235
-
236
- .. code-block :: yaml
237
-
238
- ' A singled-quoted string in YAML'
239
-
240
- .. tip ::
241
-
242
- In a single quoted string, a single quote ``' `` must be doubled:
243
-
244
- .. code-block :: yaml
245
-
246
- ' A single quote '' in a single-quoted string'
247
-
248
- .. code-block :: yaml
249
-
250
- " A double-quoted string in YAML\n "
251
-
252
- Quoted styles are useful when a string starts or ends with one or more
253
- relevant spaces.
254
-
255
- .. tip ::
256
-
257
- The double-quoted style provides a way to express arbitrary strings, by
258
- using ``\ `` escape sequences. It is very useful when you need to embed a
259
- ``\n `` or a unicode character in a string.
260
-
261
- When a string contains line breaks, you can use the literal style, indicated
262
- by the pipe (``| ``), to indicate that the string will span several lines. In
263
- literals, newlines are preserved:
264
-
265
- .. code-block :: yaml
266
-
267
- |
268
- \/ /| |\/| |
269
- / / | | | |__
270
-
271
- Alternatively, strings can be written with the folded style, denoted by ``> ``,
272
- where each line break is replaced by a space:
273
-
274
- .. code-block :: yaml
275
-
276
- >
277
- This is a very long sentence
278
- that spans several lines in the YAML
279
- but which will be rendered as a string
280
- without carriage returns.
281
-
282
- .. note ::
283
-
284
- Notice the two spaces before each line in the previous examples. They
285
- won't appear in the resulting PHP strings.
286
-
287
- Numbers
288
- .......
289
-
290
- .. code-block :: yaml
291
-
292
- # an integer
293
- 12
294
-
295
- .. code-block :: yaml
296
-
297
- # an octal
298
- 014
299
-
300
- .. code-block :: yaml
301
-
302
- # an hexadecimal
303
- 0xC
304
-
305
- .. code-block :: yaml
306
-
307
- # a float
308
- 13.4
309
-
310
- .. code-block :: yaml
311
-
312
- # an exponential number
313
- 1.2e+34
314
-
315
- .. code-block :: yaml
316
-
317
- # infinity
318
- .inf
319
-
320
- Nulls
321
- .....
322
-
323
- Nulls in YAML can be expressed with ``null `` or ``~ ``.
324
-
325
- Booleans
326
- ........
327
-
328
- Booleans in YAML are expressed with ``true `` and ``false ``.
329
-
330
- Dates
331
- .....
332
-
333
- YAML uses the ISO-8601 standard to express dates:
334
-
335
- .. code-block :: yaml
336
-
337
- 2001-12-14t21:59:43.10-05:00
338
-
339
- .. code-block :: yaml
340
-
341
- # simple date
342
- 2002-12-14
343
-
344
- Collections
345
- ~~~~~~~~~~~
346
-
347
- A YAML file is rarely used to describe a simple scalar. Most of the time, it
348
- describes a collection. A collection can be a sequence or a mapping of
349
- elements. Both sequences and mappings are converted to PHP arrays.
350
-
351
- Sequences use a dash followed by a space:
352
-
353
- .. code-block :: yaml
354
-
355
- - PHP
356
- - Perl
357
- - Python
358
-
359
- The previous YAML file is equivalent to the following PHP code:
360
-
361
- .. code-block :: php
362
-
363
- array('PHP', 'Perl', 'Python');
364
-
365
- Mappings use a colon followed by a space (``: `` ) to mark each key/value pair:
366
-
367
- .. code-block :: yaml
368
-
369
- PHP : 5.2
370
- MySQL : 5.1
371
- Apache : 2.2.20
372
-
373
- which is equivalent to this PHP code:
374
-
375
- .. code-block :: php
376
-
377
- array('PHP' => 5.2, 'MySQL' => 5.1, 'Apache' => '2.2.20');
378
-
379
- .. note ::
380
-
381
- In a mapping, a key can be any valid scalar.
382
-
383
- The number of spaces between the colon and the value does not matter:
384
-
385
- .. code-block :: yaml
386
-
387
- PHP : 5.2
388
- MySQL : 5.1
389
- Apache : 2.2.20
390
-
391
- YAML uses indentation with one or more spaces to describe nested collections:
392
-
393
- .. code-block :: yaml
394
-
395
- " symfony 1.0 " :
396
- PHP : 5.0
397
- Propel : 1.2
398
- " symfony 1.2 " :
399
- PHP : 5.2
400
- Propel : 1.3
401
-
402
- The following YAML is equivalent to the following PHP code:
403
-
404
- .. code-block :: php
405
-
406
- array(
407
- 'symfony 1.0' => array(
408
- 'PHP' => 5.0,
409
- 'Propel' => 1.2,
410
- ),
411
- 'symfony 1.2' => array(
412
- 'PHP' => 5.2,
413
- 'Propel' => 1.3,
414
- ),
415
- );
416
-
417
- There is one important thing you need to remember when using indentation in a
418
- YAML file: *Indentation must be done with one or more spaces, but never with
419
- tabulations *.
420
-
421
- You can nest sequences and mappings as you like:
422
-
423
- .. code-block :: yaml
424
-
425
- ' Chapter 1 ' :
426
- - Introduction
427
- - Event Types
428
- ' Chapter 2 ' :
429
- - Introduction
430
- - Helpers
431
-
432
- YAML can also use flow styles for collections, using explicit indicators
433
- rather than indentation to denote scope.
434
-
435
- A sequence can be written as a comma separated list within square brackets
436
- (``[] ``):
437
-
438
- .. code-block :: yaml
439
-
440
- [PHP, Perl, Python]
441
-
442
- A mapping can be written as a comma separated list of key/values within curly
443
- braces (``{} ``):
444
-
445
- .. code-block :: yaml
446
-
447
- { PHP: 5.2, MySQL: 5.1, Apache: 2.2.20 }
448
-
449
- You can mix and match styles to achieve a better readability:
450
-
451
- .. code-block :: yaml
452
-
453
- ' Chapter 1 ' : [Introduction, Event Types]
454
- ' Chapter 2 ' : [Introduction, Helpers]
455
-
456
- .. code-block :: yaml
457
-
458
- " symfony 1.0 " : { PHP: 5.0, Propel: 1.2 }
459
- " symfony 1.2 " : { PHP: 5.2, Propel: 1.3 }
460
-
461
- Comments
462
- ~~~~~~~~
463
-
464
- Comments can be added in YAML by prefixing them with a hash mark (``# ``):
465
-
466
- .. code-block :: yaml
467
-
468
- # Comment on a line
469
- " symfony 1.0 " : { PHP: 5.0, Propel: 1.2 } # Comment at the end of a line
470
- " symfony 1.2 " : { PHP: 5.2, Propel: 1.3 }
471
-
472
- .. note ::
473
-
474
- Comments are simply ignored by the YAML parser and do not need to be
475
- indented according to the current level of nesting in a collection.
476
-
477
215
.. _YAML : http://yaml.org/
478
216
.. _Packagist : https://packagist.org/packages/symfony/yaml
0 commit comments