I'm adding Twig support on existing apps that has been written in Volt.
I'm looking to replicate the same behavior of Volt that expose DI services: {{ config }}
or {{ this.config }}
but I'm not sure how volt do that (even by looking source).
For now my working way is to iterate over $di->getServices()
and assign each services to the view object
Is the right way to do that?
Mat
<?php
class Twig extends Engine implements EngineInterface
{
protected $twig;
public function __construct($view, $di, $options = [])
{
$this->setDI($di);
$loader = new \Twig_Loader_Filesystem($di->getView()->getViewsDir());
$this->twig = new TwigEnvironment($di, $loader, [
'debug' => $di->getEnv()->development
]);
$this->twig->addExtension( new \Twig_Extension_Debug() );
// Expose phalcon services to twig
foreach ($di->getServices() as $name => $obj) {
$view->$name = $obj;
}
parent::__construct($view, $di);
}
}