TwigでJSONデコードしたい

Extending Twig - Documentation - Twig - The flexible, fast, and secure PHP template engine

Twigを拡張して json_decode というフィルターを作ってあげる。

<?php

// ...

class MyExtension extends \Twig_Extension
{

    // ...

    public function getFilters()
    {
        return [
            new \Twig_Filter('json_decode', [$this, 'jsonDecodeFilter']),
        ];
    }

    public function jsonDecodeFilter($json = null)
    {
        if (!$json) {
            return null;
        }
        return json_decode($json, true);
    }

    // ...
}

こんな形で使用する。

{{ jsonString | json_decode }}

参考。

stackoverflow.com