First you need to add shapecode/twig-string-loader
to composer.json
:
Do it by execute composer require shapecode/twig-string-loader
or do it manually
{
"require": {
"shapecode/twig-string-loader": "^1.0"
}
}
Add the string loader to your $twig
object
<?php
// index.php
//...
$loader1 = new \Twig\Loader\FilesystemLoader('/path/to/templates');
$loader2 = new \Shapecode\Twig\Loader\StringLoader();
$loader = new \Twig\Loader\ChainLoader([$loader1, $loader2]);
$twig = new \Twig\Environment($loader);
Now you can compile strings with twig:
<?php
$twig->render('Hello {{ world }}', array(
'world' => 'World'
));