Hi,
I stored a list of image names in JSON format in a table, would like to use the names inside TWIG. I think the best way is to add json_decode to a twig filter and decode json inside twig.
json string names stored in a table
["01.png","02.png","03.png","04.png"]
I tried to add a twigfilter
getting this error…Call to undefined method Twig\TwigFilter::addGlobal()
namespace App\Providers;
use App\Views\Extensions\DebugExtension;
use App\Views\Extensions\RouteExtension;
use App\Views\View;
use League\Container\ServiceProvider\AbstractServiceProvider;
use Twig\Environment;
use Twig\TwigFilter;
use Twig\Loader\FilesystemLoader;
class ViewServiceProvider extends AbstractServiceProvider {
protected $provides = [
View::class
];
public function register() {
$container = $this->getContainer();
$config = $container->get('config');
$container->share(View::class, function () use ($config, $container) {
$loader = new FilesystemLoader(base_path('resources/views'));
$twig = new Environment($loader, [
'cache' => $config->get('twig.cache'),
'debug' => $config->get('twig.debug')
]);
$twig->addExtension(new RouteExtension(
$container->get('router'),
$container->get('request')->getUri()
));
$twig->addExtension(new DebugExtension);
$twig->addFilter(new TwigFilter('json_decode', function ($string) {
return json_decode($string);
}));
foreach ($config->get('twig.globals') as $key => $value) {
$twig->addGlobal($key, $value);
}
return new View($twig);
});
}
}