Roman Gelembjuk
Roman Gelembjuk

Reputation: 2005

Twig enable cache after loading Environment

I want to enable caching in Twig. I found how to do this during creating of a Twig Environment object

$twig = new Twig_Environment($loader, array(
    'cache' => '/path/to/compilation_cache',
));

But i want to add caching after object is constructed. Now can i do this?

I have the class that inherits Twig

class MyTwig extends Twig_Environment {
    public function someFunction() {
         // enable cache there
    }
}

I need to enable cache inside the function someFunction()

Upvotes: 0

Views: 388

Answers (1)

viarnes
viarnes

Reputation: 2048

Take a look inside Environment.php, there is a method called setCache. So I guess you can simply:

public function loadTwig() {
    // ...
    $this->twig = new Twig_Environment($loader, $params);
    // ...
}

public function someFunction() {
    // ...
    $this->twig->setCache('/path/to/compilation_cache');
    // ...
}

Upvotes: 2

Related Questions