Optimisation

<?php

use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
use Twig\Extra\Cache\CacheExtension;
use Twig\Extra\Cache\CacheRuntime;
use Twig\RuntimeLoader\RuntimeLoaderInterface;

add_filter('timber/twig', function ($twig) {
    $twig->addRuntimeLoader(new class implements RuntimeLoaderInterface
    {
        public function load($class)
        {
            if (CacheRuntime::class === $class) {
                return new CacheRuntime(new TagAwareAdapter(new FilesystemAdapter('', 0, TIMBER_LOC . '/cache/twig')));
            }
        }
    });
    $twig->addExtension(new CacheExtension());

    return $twig;
});

add_filter('timber/twig/environment/options', function ($options) {
    $options['cache'] = true;
    return $options;
});

Last updated