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));
}
}