如何添加扩展名在twig中添加全局

时间:2017-01-27 03:48:10

标签: php twig slim

当我尝试在树枝中添加扩展添加全局

$view = new \Slim\Views\Twig($settings['template_path'], [
    'debug' => $settings['debug'],
    'cache' => $settings['cache_path']
]);
// Add extensions
$view->addExtension(new \Slim\Views\TwigExtension(
    $c['router'],
    $c['request']->getUri()
));
$view->addExtension(new \Core\TwigFunction());  
$view->addExtension(new Twig_Extension_Debug());
$view = new Twig_Environment();;
$view->addGlobal('session', $_SESSION);

我收到:

  

调用未定义的方法Twig_Environment :: offsetSet()

1 个答案:

答案 0 :(得分:2)

在第

$view = new Twig_Environment();

您正在覆盖$view变量。删除该行应解决问题,因为slim/twig-view为您设置了Twig环境。