public function twigInit() { return $this->app->singleton('twig', function () { $loader = new \Twig_Loader_Filesystem(\Config::get('view::path')); if (\Config::get('view::cache')) { $env = array('cache' => \Config::get('view::cache_dir')); } $twig = new \Twig_Environment($loader, isset($env) ? $env : array()); //开启twig的cache后默认不显示twig数据收集。这里有一点小bug if ($this->getContainer()->isDebug() && empty($env)) { $twig = new \DebugBar\Bridge\Twig\TraceableTwigEnvironment($twig); $this->app->singleton('debugbar')->addCollector(new \DebugBar\Bridge\Twig\TwigCollector($twig)); } $twig->addExtension(new WebExtension()); $extensions = \Config::get('view::extensions'); foreach ($extensions as $extension) { if ($this->getContainer()->buildMoudle($extension)->isSubclassOf('Twig_Extension')) { $twig->addExtension(new $extension()); } } return $twig; }); }
<?php include __DIR__ . '/vendor/autoload.php'; include __DIR__ . '/../../bootstrap.php'; $debugbarRenderer->setBaseUrl('../../../src/DebugBar/Resources'); $loader = new Twig_Loader_Filesystem('.'); $twig = new DebugBar\Bridge\Twig\TraceableTwigEnvironment(new Twig_Environment($loader), $debugbar['time']); $debugbar->addCollector(new DebugBar\Bridge\Twig\TwigCollector($twig)); render_demo_page(function () use($twig) { echo $twig->render('hello.html', array('name' => 'peter pan')); });