CodeIgniter如何在树枝模板中访问会话变量 [英] CodeIgniter How to access session variables in twig templates

查看:536
本文介绍了CodeIgniter如何在树枝模板中访问会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

redirect('app'); // app是一个渲染模板视图的控制器。

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

  {{session.userdata.nick}} 
pre>

但它显示为空字符串。



感谢提前。

解决方案

我正在使用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); //加载配置文件

//设置包含twig的路径
ini_set('include_path',ini_get('include_path')。PATH_SEPARATOR。APPPATH。'third_party / Twig / lib / Twig' );
require_once(string)'Autoloader.php';

//注册自动加载器
Twig_Autoloader :: register();
log_message('debug','twig autoloader loaded');

//初始路径
$ this-> template_dir = $ this-> _ci-> config-> item('template_dir');
$ this-> cache_dir = $ this-> _ci-> config-> item('cache_dir');

//加载环境
$ 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'=&

// ADD SESSION TO TWIG - JZ
$ this-> _twig_env-> addGlobal('session',$ this-> _ci-> session);
//会议现在可在TWIG模板中使用!

$ this-> ci_function_init();现在我们已经将我们的会话加载到我们的twig实例中了,我们可以看到,我们访问会话变量(例如CI userdata )。

 < span> __ ci_last_regenerate:{{session.userdata。 __ci_last_regenerate}}< / span> 

php twig codeigniter


somebody can help me please. I'm new in CodeIgniter and Twig. I have declared in my controller the following:

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

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

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

then, the question is: How can I get those variables from the twig template? I tried using:

{{ session.userdata.nick }}

but it shows like empty string.

thanks by advance.

解决方案

I'm using CodeIgniter 3RC3 and the Twig-Codeigniter library (thanks Erik & Bennet!).

To enable easy session access in twig, I added one line to the /application/libraries/Twig.php file's __construct() method:

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();

}

Now that we have our session loaded into our twig instance, we access session variables (such as CI's userdata) in our twig templates like so:

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

这篇关于CodeIgniter如何在树枝模板中访问会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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