Accessing session from TWIG template

后端 未结 5 561
长发绾君心
长发绾君心 2020-11-29 22:26

I\'ve searched a lot on the net how to access the global $_SESSION array from TWIG template and found this: {{app.session.get(\'index\')}}, but whe

5条回答
  •  天命终不由人
    2020-11-29 22:38

    Setup twig

    $twig = new Twig_Environment(...);    
    $twig->addGlobal('session', $_SESSION);
    

    Then within your template access session values for example

    $_SESSION['username'] in php file Will be equivalent to {{ session.username }} in your twig template
    

提交回复
热议问题