Question

I spend quite some time trying to figure out how to "compile" all my Twig templates in a Slim based application, to make sure all strings were ready to be picked up by xgettext for further processing and translation.

It turned out to be quite easy as soon as I had the right pieces of information put together, but I couldn't find any place on the internet telling me how to do exactly this with Twig in a Slim application.

Was it helpful?

Solution

The Twig documentation has a fine example of how to extract template strings. However, before you're able to do that, you need to pull the Twig environment out of your Slim application, as described in the Slim Knowledge Base.

So, here's a modified version of the example from Twig documentation:

// Specify where your templates are located.
$tplDir = '/path/to/your/templates';

// Get the Twig environment from your Slim app, $app.
$twig = $app->view()->getEnvironment();

// Iterate over all your templates.
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tplDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file)
{
    // Force compilation.
    if ($file->isFile()) {
        $twig->loadTemplate(str_replace($tplDir.'/', '', $file));
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top