Skip to content

Commit c418b22

Browse files
committed
[twig reference] add examples in yaml part
1 parent 7ad040b commit c418b22

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
@@ -474,8 +474,45 @@ yaml_encode
474474
``dumpObjects`` *(optional)*
475475
**type**: ``boolean`` **default**: ``false``
476476

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

480517
yaml_dump
481518
~~~~~~~~~
@@ -494,6 +531,42 @@ yaml_dump
494531
Does the same as `yaml_encode() <yaml_encode>`_, but includes the type in
495532
the output.
496533

534+
The ``inline`` argument is the level where you switch to inline YAML:
535+
536+
.. code-block:: twig
537+
538+
{% set array = {
539+
'a': {
540+
'c': 'e'
541+
},
542+
'b': {
543+
'd': 'f'
544+
}
545+
} %}
546+
547+
{{ array|yaml_dump(0) }}
548+
{# output: %array% { a: { c: e }, b: { d: f } } #}
549+
550+
{{ array|yaml_dump(1) }}
551+
{# output: %array% a: { c: e } b: { d: f } #}
552+
553+
The ``dumpObjects`` argument is used to dump objects:
554+
555+
.. code-block:: php
556+
557+
// ...
558+
$object = new \stdClass();
559+
$object->foo = 'bar';
560+
// ...
561+
562+
.. code-block:: twig
563+
564+
{{ object|yaml_dump(0, false) }}
565+
{# output: %object% null #}
566+
567+
{{ object|yaml_dump(0, true) }}
568+
{# output: %object% !php/object 'O:8:"stdClass":1:{s:3:"foo";s:3:"bar";}' #}
569+
497570
abbr_class
498571
~~~~~~~~~~
499572

0 commit comments

Comments
 (0)