CodeIgniter: Add Session in Twig


To add the session variable on your twig template, you have to add the following line in your Twig library or controller.

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

Then on your twig template, you can print the session like this

{{ session.userdata.username }}

Since in CodeIgniter, the session stored by user is usually in the userdata array. Otherwise, you can just simply call the session variable and name

{{ session.username }}

Comments