Skip to content

Commit 9d5fe51

Browse files
committed
[twig reference] add examples in yaml part
1 parent da70070 commit 9d5fe51

File tree

1 file changed

+75
-2
lines changed

1 file changed

+75
-2
lines changed

reference/twig_reference.rst

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,45 @@ yaml_encode
483483
``dumpObjects`` *(optional)*
484484
**type**: ``boolean`` **default**: ``false``
485485

486-
Transforms the input into YAML syntax. See :ref:`components-yaml-dump` for
487-
more information.
486+
Transforms the input into YAML syntax.
487+
488+
The ``inline`` argument is the level where you switch to inline YAML:
489+
490+
.. code-block:: twig
491+
492+
{% set array = {
493+
'a': {
494+
'c': 'e'
495+
},
496+
'b': {
497+
'd': 'f'
498+
}
499+
} %}
500+
501+
{{ array|yaml_encode(0) }}
502+
{# output: { a: { c: e }, b: { d: f } } #}
503+
504+
{{ array|yaml_encode(1) }}
505+
{# output: a: { c: e } b: { d: f } #}
506+
507+
The ``dumpObjects`` argument is used to dump objects:
508+
509+
.. code-block:: php
510+
511+
// ...
512+
$object = new \stdClass();
513+
$object->foo = 'bar';
514+
// ...
515+
516+
.. code-block:: twig
517+
518+
{{ object|yaml_encode(0, false) }}
519+
{# output: null #}
520+
521+
{{ object|yaml_encode(0, true) }}
522+
{# output: !php/object 'O:8:"stdClass":1:{s:5:"foo";s:7:"bar";}' #}
523+
524+
See :ref:`components-yaml-dump` for more information.
488525

489526
yaml_dump
490527
~~~~~~~~~
@@ -503,6 +540,42 @@ yaml_dump
503540
Does the same as `yaml_encode() <yaml_encode>`_, but includes the type in
504541
the output.
505542

543+
The ``inline`` argument is the level where you switch to inline YAML:
544+
545+
.. code-block:: twig
546+
547+
{% set array = {
548+
'a': {
549+
'c': 'e'
550+
},
551+
'b': {
552+
'd': 'f'
553+
}
554+
} %}
555+
556+
{{ array|yaml_dump(0) }}
557+
{# output: %array% { a: { c: e }, b: { d: f } } #}
558+
559+
{{ array|yaml_dump(1) }}
560+
{# output: %array% a: { c: e } b: { d: f } #}
561+
562+
The ``dumpObjects`` argument is used to dump objects:
563+
564+
.. code-block:: php
565+
566+
// ...
567+
$object = new \stdClass();
568+
$object->foo = 'bar';
569+
// ...
570+
571+
.. code-block:: twig
572+
573+
{{ object|yaml_dump(0, false) }}
574+
{# output: %object% null #}
575+
576+
{{ object|yaml_dump(0, true) }}
577+
{# output: %object% !php/object 'O:8:"stdClass":1:{s:3:"foo";s:3:"bar";}' #}
578+
506579
abbr_class
507580
~~~~~~~~~~
508581

0 commit comments

Comments
 (0)