【问题标题】:CodeIgniter How to access session variables in twig templatesCodeIgniter 如何访问 twig 模板中的会话变量
【发布时间】:2014-06-10 18:27:34
【问题描述】:

请有人可以帮助我。我是 CodeIgniter 和 Twig 的新手。我在我的控制器中声明了以下内容:

$datasession = array(
'nick' => $sess_nick,
'login_ok' => true
);

$this->session->set_userdata($datasession);

redirect('app'); //app is a controller that render the template view.

那么,问题是:如何从 twig 模板中获取这些变量?我尝试使用:

{{ session.userdata.nick }}

但它显示为空字符串。

提前致谢。

【问题讨论】:

    标签: php codeigniter session variables twig


    【解决方案1】:

    要在 twig 模板上添加会话变量,您必须在 Twig 库或控制器中添加以下行。

    $datasession = array(
        'nick' => $sess_nick,
        'login_ok' => true
    );
    $this->session->set_userdata($datasession);
    $this->_twig->addGlobal("session", $this->CI->session);
    

    然后在你的树枝模板上,你可以像这样打印会话

    {{ session.userdata.nick }}
    

    由于在 CodeIgniter 中,用户存储的会话通常在 userdata 数组中。否则,您可以简单地调用会话变量和名称

    {{ session.nick }}
    

    源:http://llanalewis.blogspot.co.uk/2013/08/codeigniter-add-session-in-twig.html

    【讨论】:

    • 致命错误:在第 151 行调用 C:\AppServ\www\curso\ApplicationCI\application\controllers\putocontrolador.php 中未定义的方法 Twig::addGlobal()
    • 我检查了库文件夹中的 Twig.php 文件,发现没有这种称为 addGlobal 的方法。我现在能做什么?
    • 你能检查一下 Twig 是否安装和使用正确吗?查看本指南llanalewis.blogspot.co.uk/2013/08/…
    • P.S. Twig::addGlobal() 方法确实存在。但我认为你必须得到它的一个实例。看看这个也twig.sensiolabs.org/doc/advanced.html#globals
    • @Latheesan 使用 CI 3 进行这项工作,我已经尝试过了,但没有显示任何内容!
    【解决方案2】:

    好的,感谢 Latheesan Kanes 的帮助。你的指导很有帮助。我想分享一下我解决这个问题的方法。

    正如 Latheesan 提到的,我们必须使用 addGlobal() 方法(我在我的 Twig 库文件夹中添加了这个方法)

    如下:

    $this->_twig->addGlobal("session", $this->CI->session);
    

    但不要忘记在加载 Session 库之前。这边。

    $this->CI->load->library('session');
    

    这样,您可以在所有树枝视图中全局地进行会话。

    【讨论】:

      【解决方案3】:

      我正在使用 CodeIgniter 3RC3 和 Twig-Codeigniter library(感谢 Erik 和 Bennet!)。

      为了在 twig 中轻松访问会话,我在 /application/libraries/Twig.php 文件的 __construct() 方法中添加了一行:

      public function __construct()
      {
          $this->_ci = & get_instance();
          $this->_ci->config->load(self::TWIG_CONFIG_FILE); // load config file
      
          // set include path for twig
          ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'third_party/Twig/lib/Twig');
          require_once (string)'Autoloader.php';
      
          // register autoloader
          Twig_Autoloader::register();
          log_message('debug', 'twig autoloader loaded');
      
          // init paths
          $this->template_dir = $this->_ci->config->item('template_dir');
          $this->cache_dir = $this->_ci->config->item('cache_dir');
      
          // load environment
          $loader = new Twig_Loader_Filesystem($this->template_dir, $this->cache_dir);
          $this->_twig_env = new Twig_Environment($loader, array(
                                                  'cache' => $this->cache_dir,
                                                  'auto_reload' => TRUE));
      
          // ADD SESSION TO TWIG - JZ
          $this->_twig_env->addGlobal('session', $this->_ci->session);
          // SESSION IS NOW AVAILABLE IN TWIG TEMPLATES!
      
          $this->ci_function_init();
      
      }
      

      现在我们已将会话加载到 twig 实例中,我们可以像这样在 twig 模板中访问会话变量(例如 CI 的 userdata):

      <span>__ci_last_regenerate: {{ session.userdata.__ci_last_regenerate }}</span>
      

      【讨论】:

      • 这也适用于 CodeIgniter 3.0+ 的发行版。
      【解决方案4】:

      我通过以下代码在 Twig 中成功使用了会话变量:

      在控制器中:

      $datasession = array(
          'language' => "PHP",
          'framework' => "CodeIgniter"
      );
      
      $this->session->set_userdata($datasession);
      $this->twig->addGlobal("session", $this->session);
      

      在模板中:

      {{ session.language }} 
      {{ session.framework }} 
      

      我正在使用https://github.com/kenjis/codeigniter-ss-twig

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-23
        • 1970-01-01
        • 2011-02-02
        • 2012-10-25
        • 2012-05-04
        • 1970-01-01
        相关资源
        最近更新 更多