Welcome Guest, Not a member yet? Register   Sign In
Session variables in smarty with CI
#1

[eluser]Abhijeet[/eluser]
Hello
I m facing a small problem while showing the session variables as a smarty way as follows

Code:
{$smarty.session.id}

I m trying to set the session data in one of the controller functions and load the smarty template.
but while displaying the session data it is showing blank

is there any way to show the same using smarty way......


Thanks in advance

Abhijeet
#2

[eluser]Abhijeet[/eluser]
No Reply still.....

Anyways I found a way to solve out but not the perfect way

still waiting for the reply

and thanks in advance

Abhijeet
#3

[eluser]dmitrybelyakov[/eluser]
Maybe you can pass session variables as regular variables from your controller to Smarty parser?
#4

[eluser]Abhijeet[/eluser]
that is true

but is there any way to do it with smarty way

i have done it finally through this way only

but i want to use the full smarty features...... thats y asking .... :-)
#5

[eluser]dmitrybelyakov[/eluser]
What smarty features do you intend to use? After some exploration i (yet) see no way to acces session directly from smarty templates.
So you still need to pass session data to smarty template somehow.

You can do it in several ways:


1. In your controllers like this:

Code:
$data['session']=$this->session->userdata;
$this->smarty_parser->parse("smarty.php", $data);

Or if you don't whant to do it each time in your controllers, you can move this to your smarty wrapper class.

2. In your smarty wrapper (smarty_parser.php) like this:

Code:
function parse($template, $data, $return = FALSE)
    {      
        if ($template == '') return FALSE;
        $CI =& get_instance();
        
        /* here it goes */
        $data['session']=$CI->session->userdata;
        
        
       ...
    }

Then you can access all your session data in templates by using
Code:
{$session.session_id}


Hope that helps :-)
#6

[eluser]Abhijeet[/eluser]
Thanks buddy
will try this for sure


thanks again.......

Abhijeet
#7

[eluser]Jagar[/eluser]
I have opened a similar thread as this one, but after reading the CI user_guide about sessions, it states that CI does not use native php session, but rather cookies. if you do in smarty
Code:
{$smarty.cookies.ci_sessions
i think it's an s at the end of session. You will see some ugly stuff.
#8

[eluser]Jagar[/eluser]
I don't know how you have setup your smarty with CI, but if you have this line somewhere

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

require(APPPATH.'libraries/smarty/Smarty.class.php');

class Smarty_Parser extends Smarty {
    
    function __construct($config = array()){
        parent::Smarty();
        
        if (count($config)>0) {
            $this->initialize($config);
        }
        
        //register Smarty resource_name 'ci'
        $this->register_resource('ci',array($this, 'ci_get_template', 'ci_get_timestamp','ci_get_secure','ci_get_trusted'));

And you have session as autoload, then you can use the following to access the session from smarty.

[code]
<h1>{$CI->session->userdata('session_name')}</h1>

That will print the CI session.




Theme © iAndrew 2016 - Forum software by © MyBB