From 346ea4bc6bf67bbaa6c0702edfefc1c83a0bfb53 Mon Sep 17 00:00:00 2001 From: sarah-eit Date: Thu, 7 Mar 2024 14:24:36 +0100 Subject: [PATCH] [Twig] [twig reference] add examples in yaml part --- reference/twig_reference.rst | 73 +++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/reference/twig_reference.rst b/reference/twig_reference.rst index 97ee6a57185..fdb9e27ccf2 100644 --- a/reference/twig_reference.rst +++ b/reference/twig_reference.rst @@ -483,8 +483,43 @@ yaml_encode ``dumpObjects`` *(optional)* **type**: ``boolean`` **default**: ``false`` -Transforms the input into YAML syntax. See :ref:`components-yaml-dump` for -more information. +Transforms the input into YAML syntax. + +The ``inline`` argument is the level where you switch to inline YAML: + +.. code-block:: twig + + {% set array = { + 'a': { + 'c': 'e' + }, + 'b': { + 'd': 'f' + } + } %} + + {{ array|yaml_encode(inline = 0) }} + {# output: { a: { c: e }, b: { d: f } } #} + + {{ array|yaml_encode(inline = 1) }} + {# output: a: { c: e } b: { d: f } #} + +The ``dumpObjects`` argument is used to dump objects:: + + // ... + $object = new \stdClass(); + $object->foo = 'bar'; + // ... + +.. code-block:: twig + + {{ object|yaml_encode(dumpObjects = false) }} + {# output: null #} + + {{ object|yaml_encode(dumpObjects = true) }} + {# output: !php/object 'O:8:"stdClass":1:{s:5:"foo";s:7:"bar";}' #} + +See :ref:`components-yaml-dump` for more information. yaml_dump ~~~~~~~~~ @@ -503,6 +538,40 @@ yaml_dump Does the same as `yaml_encode() `_, but includes the type in the output. +The ``inline`` argument is the level where you switch to inline YAML: + +.. code-block:: twig + + {% set array = { + 'a': { + 'c': 'e' + }, + 'b': { + 'd': 'f' + } + } %} + + {{ array|yaml_dump(inline = 0) }} + {# output: %array% { a: { c: e }, b: { d: f } } #} + + {{ array|yaml_dump(inline = 1) }} + {# output: %array% a: { c: e } b: { d: f } #} + +The ``dumpObjects`` argument is used to dump objects:: + + // ... + $object = new \stdClass(); + $object->foo = 'bar'; + // ... + +.. code-block:: twig + + {{ object|yaml_dump(dumpObjects = false) }} + {# output: %object% null #} + + {{ object|yaml_dump(dumpObjects = true) }} + {# output: %object% !php/object 'O:8:"stdClass":1:{s:3:"foo";s:3:"bar";}' #} + abbr_class ~~~~~~~~~~