Skip to content

Commit 5dee2a2

Browse files
committed
Merge branch '6.0' into 6.1
* 6.0: Add missing redirection Merge YAML docs into one
2 parents 5846009 + 9eb3bd8 commit 5dee2a2

File tree

4 files changed

+348
-347
lines changed

4 files changed

+348
-347
lines changed

_build/redirection_map

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,8 @@
538538
/components/security/firewall /security#the-firewall
539539
/components/security/secure_tools /security/passwords
540540
/components/security /security
541-
/components/var_dumper/advanced /components/var_dumper
541+
/components/var_dumper/advanced /components/var_dumper#advanced-usage
542+
/components/yaml/yaml_format /components/yaml#yaml-format
542543
/email /mailer
543544
/frontend/assetic /frontend
544545
/frontend/assetic/index /frontend

components/yaml.rst

Lines changed: 329 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ the `YAML 1.2 version specification`_.
2323

2424
.. tip::
2525

26-
Learn more about the Yaml component in the
27-
:doc:`/components/yaml/yaml_format` article.
26+
Learn more about :ref:`YAML specifications <yaml-format>`.
2827

2928
Installation
3029
------------
@@ -448,14 +447,336 @@ Add the ``--format`` option to get the output in JSON format:
448447
YAML files. This may for example be useful for recognizing deprecations of
449448
contents of YAML files during automated tests.
450449

451-
Learn More
452-
----------
450+
.. _yaml-format:
453451

454-
.. toctree::
455-
:maxdepth: 1
456-
:glob:
452+
.. index::
453+
single: Yaml; YAML Format
454+
455+
The YAML Format
456+
---------------
457+
458+
Scalars
459+
~~~~~~~
460+
461+
The syntax for scalars is similar to the PHP syntax.
462+
463+
Strings
464+
.......
465+
466+
Strings in YAML can be wrapped both in single and double quotes. In some cases,
467+
they can also be unquoted:
468+
469+
.. code-block:: yaml
470+
471+
A string in YAML
472+
473+
'A single-quoted string in YAML'
474+
475+
"A double-quoted string in YAML"
476+
477+
Quoted styles are useful when a string starts or end with one or more relevant
478+
spaces, because unquoted strings are trimmed on both end when parsing their
479+
contents. Quotes are required when the string contains special or reserved characters.
480+
481+
When using single-quoted strings, any single quote ``'`` inside its contents
482+
must be doubled to escape it:
483+
484+
.. code-block:: yaml
485+
486+
'A single quote '' inside a single-quoted string'
487+
488+
Strings containing any of the following characters must be quoted. Although you
489+
can use double quotes, for these characters it is more convenient to use single
490+
quotes, which avoids having to escape any backslash ``\``:
491+
492+
* ``:``, ``{``, ``}``, ``[``, ``]``, ``,``, ``&``, ``*``, ``#``, ``?``, ``|``,
493+
``-``, ``<``, ``>``, ``=``, ``!``, ``%``, ``@``, `````
494+
495+
The double-quoted style provides a way to express arbitrary strings, by
496+
using ``\`` to escape characters and sequences. For instance, it is very useful
497+
when you need to embed a ``\n`` or a Unicode character in a string.
498+
499+
.. code-block:: yaml
500+
501+
"A double-quoted string in YAML\n"
502+
503+
If the string contains any of the following control characters, it must be
504+
escaped with double quotes:
505+
506+
* ``\0``, ``\x01``, ``\x02``, ``\x03``, ``\x04``, ``\x05``, ``\x06``, ``\a``,
507+
``\b``, ``\t``, ``\n``, ``\v``, ``\f``, ``\r``, ``\x0e``, ``\x0f``, ``\x10``,
508+
``\x11``, ``\x12``, ``\x13``, ``\x14``, ``\x15``, ``\x16``, ``\x17``, ``\x18``,
509+
``\x19``, ``\x1a``, ``\e``, ``\x1c``, ``\x1d``, ``\x1e``, ``\x1f``, ``\N``,
510+
``\_``, ``\L``, ``\P``
511+
512+
Finally, there are other cases when the strings must be quoted, no matter if
513+
you're using single or double quotes:
514+
515+
* When the string is ``true`` or ``false`` (otherwise, it would be treated as a
516+
boolean value);
517+
* When the string is ``null`` or ``~`` (otherwise, it would be considered as a
518+
``null`` value);
519+
* When the string looks like a number, such as integers (e.g. ``2``, ``14``, etc.),
520+
floats (e.g. ``2.6``, ``14.9``) and exponential numbers (e.g. ``12e7``, etc.)
521+
(otherwise, it would be treated as a numeric value);
522+
* When the string looks like a date (e.g. ``2014-12-31``) (otherwise it would be
523+
automatically converted into a Unix timestamp).
524+
525+
When a string contains line breaks, you can use the literal style, indicated
526+
by the pipe (``|``), to indicate that the string will span several lines. In
527+
literals, newlines are preserved:
528+
529+
.. code-block:: yaml
530+
531+
|
532+
\/ /| |\/| |
533+
/ / | | | |__
534+
535+
Alternatively, strings can be written with the folded style, denoted by ``>``,
536+
where each line break is replaced by a space:
537+
538+
.. code-block:: yaml
539+
540+
>
541+
This is a very long sentence
542+
that spans several lines in the YAML.
543+
544+
# This will be parsed as follows: (notice the trailing \n)
545+
# "This is a very long sentence that spans several lines in the YAML.\n"
546+
547+
>-
548+
This is a very long sentence
549+
that spans several lines in the YAML.
550+
551+
# This will be parsed as follows: (without a trailing \n)
552+
# "This is a very long sentence that spans several lines in the YAML."
553+
554+
.. note::
555+
556+
Notice the two spaces before each line in the previous examples. They
557+
won't appear in the resulting PHP strings.
558+
559+
Numbers
560+
.......
561+
562+
.. code-block:: yaml
563+
564+
# an integer
565+
12
566+
567+
.. code-block:: yaml
568+
569+
# an octal
570+
0o14
571+
572+
.. code-block:: yaml
573+
574+
# an hexadecimal
575+
0xC
576+
577+
.. code-block:: yaml
578+
579+
# a float
580+
13.4
581+
582+
.. code-block:: yaml
583+
584+
# an exponential number
585+
1.2e+34
586+
587+
.. code-block:: yaml
588+
589+
# infinity
590+
.inf
591+
592+
Nulls
593+
.....
594+
595+
Nulls in YAML can be expressed with ``null`` or ``~``.
596+
597+
Booleans
598+
........
599+
600+
Booleans in YAML are expressed with ``true`` and ``false``.
601+
602+
Dates
603+
.....
604+
605+
YAML uses the `ISO-8601`_ standard to express dates:
606+
607+
.. code-block:: yaml
608+
609+
2001-12-14T21:59:43.10-05:00
610+
611+
.. code-block:: yaml
612+
613+
# simple date
614+
2002-12-14
615+
616+
.. _yaml-format-collections:
617+
618+
Collections
619+
~~~~~~~~~~~
620+
621+
A YAML file is rarely used to describe a simple scalar. Most of the time, it
622+
describes a collection. YAML collections can be a sequence (indexed arrays in PHP)
623+
or a mapping of elements (associative arrays in PHP).
624+
625+
Sequences use a dash followed by a space:
626+
627+
.. code-block:: yaml
628+
629+
- PHP
630+
- Perl
631+
- Python
632+
633+
The previous YAML file is equivalent to the following PHP code::
634+
635+
['PHP', 'Perl', 'Python'];
636+
637+
Mappings use a colon followed by a space (``:`` ) to mark each key/value pair:
638+
639+
.. code-block:: yaml
640+
641+
PHP: 5.2
642+
MySQL: 5.1
643+
Apache: 2.2.20
644+
645+
which is equivalent to this PHP code::
646+
647+
['PHP' => 5.2, 'MySQL' => 5.1, 'Apache' => '2.2.20'];
648+
649+
.. note::
650+
651+
In a mapping, a key can be any valid scalar.
652+
653+
The number of spaces between the colon and the value does not matter:
654+
655+
.. code-block:: yaml
656+
657+
PHP: 5.2
658+
MySQL: 5.1
659+
Apache: 2.2.20
660+
661+
YAML uses indentation with one or more spaces to describe nested collections:
662+
663+
.. code-block:: yaml
664+
665+
'symfony 1.0':
666+
PHP: 5.0
667+
Propel: 1.2
668+
'symfony 1.2':
669+
PHP: 5.2
670+
Propel: 1.3
671+
672+
The above YAML is equivalent to the following PHP code::
673+
674+
[
675+
'symfony 1.0' => [
676+
'PHP' => 5.0,
677+
'Propel' => 1.2,
678+
],
679+
'symfony 1.2' => [
680+
'PHP' => 5.2,
681+
'Propel' => 1.3,
682+
],
683+
];
684+
685+
There is one important thing you need to remember when using indentation in a
686+
YAML file: *Indentation must be done with one or more spaces, but never with
687+
tabulators*.
688+
689+
You can nest sequences and mappings as you like:
690+
691+
.. code-block:: yaml
692+
693+
'Chapter 1':
694+
- Introduction
695+
- Event Types
696+
'Chapter 2':
697+
- Introduction
698+
- Helpers
699+
700+
YAML can also use flow styles for collections, using explicit indicators
701+
rather than indentation to denote scope.
702+
703+
A sequence can be written as a comma separated list within square brackets
704+
(``[]``):
705+
706+
.. code-block:: yaml
707+
708+
[PHP, Perl, Python]
709+
710+
A mapping can be written as a comma separated list of key/values within curly
711+
braces (``{}``):
712+
713+
.. code-block:: yaml
714+
715+
{ PHP: 5.2, MySQL: 5.1, Apache: 2.2.20 }
716+
717+
You can mix and match styles to achieve a better readability:
718+
719+
.. code-block:: yaml
720+
721+
'Chapter 1': [Introduction, Event Types]
722+
'Chapter 2': [Introduction, Helpers]
723+
724+
.. code-block:: yaml
725+
726+
'symfony 1.0': { PHP: 5.0, Propel: 1.2 }
727+
'symfony 1.2': { PHP: 5.2, Propel: 1.3 }
728+
729+
Comments
730+
~~~~~~~~
731+
732+
Comments can be added in YAML by prefixing them with a hash mark (``#``):
733+
734+
.. code-block:: yaml
735+
736+
# Comment on a line
737+
"symfony 1.0": { PHP: 5.0, Propel: 1.2 } # Comment at the end of a line
738+
"symfony 1.2": { PHP: 5.2, Propel: 1.3 }
739+
740+
.. note::
741+
742+
Comments are ignored by the YAML parser and do not need to be indented
743+
according to the current level of nesting in a collection.
744+
745+
Explicit Typing
746+
~~~~~~~~~~~~~~~
747+
748+
The YAML specification defines some tags to set the type of any data explicitly:
749+
750+
.. code-block:: yaml
457751
458-
yaml/*
752+
data:
753+
# this value is parsed as a string (it's not transformed into a DateTime)
754+
start_date: !!str 2002-12-14
755+
756+
# this value is parsed as a float number (it will be 3.0 instead of 3)
757+
price: !!float 3
758+
759+
# this value is parsed as binary data encoded in base64
760+
picture: !!binary |
761+
R0lGODlhDAAMAIQAAP//9/X
762+
17unp5WZmZgAAAOfn515eXv
763+
Pz7Y6OjuDg4J+fn5OTk6enp
764+
56enmleECcgggoBADs=
765+
766+
Unsupported YAML Features
767+
~~~~~~~~~~~~~~~~~~~~~~~~~
768+
769+
The following YAML features are not supported by the Symfony Yaml component:
770+
771+
* Multi-documents (``---`` and ``...`` markers);
772+
* Complex mapping keys and complex values starting with ``?``;
773+
* Tagged values as keys;
774+
* The following tags and types: ``!!set``, ``!!omap``, ``!!pairs``, ``!!seq``,
775+
``!!bool``, ``!!int``, ``!!merge``, ``!!null``, ``!!timestamp``, ``!!value``, ``!!yaml``;
776+
* Tags (``TAG`` directive; example: ``%TAG ! tag:example.com,2000:app/``)
777+
and tag references (example: ``!<tag:example.com,2000:app/foo>``);
778+
* Using sequence-like syntax for mapping elements (example: ``{foo, bar}``; use
779+
``{foo: ~, bar: ~}`` instead).
459780

460781
.. _`YAML`: https://yaml.org/
461782
.. _`YAML 1.2 version specification`: https://yaml.org/spec/1.2/spec.html

0 commit comments

Comments
 (0)