将会话传递到TWIG模板 [英] Passing Session to TWIG template

查看:91
本文介绍了将会话传递到TWIG模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想使用超薄微型框架在树枝模板中获取$_SESSION['session'];时遇到问题.

i have a problem when i want to get $_SESSION['session']; in twig template using slim micro Framework.

这是我的代码:

<!DOCTYPE html>
   <html>
      <head>
         <title>{{ title }} </title>
      </head>

     <body>
      <p> welcome <?php echo $_SESSION['username']; ?>                                                                                                                                       
         <p> {{ body }} </p>
       <a href="http://localhost/slim/public_html/logout">logout</a>
     </body>
  </html>

我无法使用该代码获取会话用户名.

i can't get session username with that code.

关于如何将会话传递到树枝模板的任何建议?

any suggestion how to passing session to twig template?

推荐答案

您应该将会话注册为全局树枝,以便在模板中可以访问它.

You should register session as a twig global, so it becomes accessible in your templates.

//$twig is a \Twig_Environment instance
$twig->addGlobal("session", $_SESSION);

在您的模板中:

{{ session.username }}

这篇关于将会话传递到TWIG模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆