From 1f42ed54c2ab07608302a26cc2e97a4f9ae7e324 Mon Sep 17 00:00:00 2001 From: Marc Egger Date: Mon, 9 Sep 2019 15:36:24 +0200 Subject: [PATCH] add json decode filter to twig --- Documentation/Manual.rst | 13 +++++++++++++ extension/Classes/Core/Report/Report.php | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/Documentation/Manual.rst b/Documentation/Manual.rst index cfb8d734..ec82c20a 100644 --- a/Documentation/Manual.rst +++ b/Documentation/Manual.rst @@ -5450,6 +5450,19 @@ using the `qfqlink` filter:: will render a link to *http://www.example.com*. +Json Decode +^^^^^^^^^^^ +A String can be JSON decoded in Twig the following way:: + + {% set decoded = '["this is one", "this is two"]' | json_decode%} + +This can then be used as a normal object in Twig:: + + {{ decoded[0] }} + +will render *this is one*. + + Available Store Variables ^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/extension/Classes/Core/Report/Report.php b/extension/Classes/Core/Report/Report.php index 784bf749..4fa4bacd 100644 --- a/extension/Classes/Core/Report/Report.php +++ b/extension/Classes/Core/Report/Report.php @@ -712,6 +712,13 @@ class Report { }, ['is_safe' => ['html']]); $twig->addFilter($filter); + // Json decode Filter + // E.g.: {% set obj = '["this is one", "this is two"]' | json_decode%} + $filter = new \Twig\TwigFilter('json_decode', function ($string) { + return json_decode($string); + }, ['is_safe' => ['html']]); + $twig->addFilter($filter); + // render Twig $contentTwig = $twig->render($twig_template, array( 'context' => $resultAssoc, // backward compatibility for MNF -- GitLab