Access CI session from smarty templates

#1
[eluser]Jagar[/eluser]
How do I access the CI session from smarty templates.

For example before I moved to CI I accessed the php session as
Code:
{$smarty.session.session_name}


how is this accomplished in CI, uses another method for sessions.

Thanks

#2
[eluser]Jagar[/eluser]
I have a left menu where it shows additional menus if the admin is logged in. The way it was used to display is that I created a
Code:
$_SESSION['admin_logged']
when the admin log in was successful.

How can I accomplish this, I created a
Code:
$this->session->set_userdata('admin_logged',$username)
in CI, but not sure how to check it in smarty or how to access the CI session.


Any help is appreciated

Thanks

#3
[eluser]Jagar[/eluser]
I found the way and it may not be the best.

I'm using smarty templating engine, and inside my libraries/smarty_parser.php inside parse function I have the following:

Code:
function parse($template, $data='', $return = false){
        if ($template == ''){
            return false;
        }

        $CI =& get_instance();
        
        $this->assign('admin_logged',$CI->session->userdata('admin_logged'));

........ more stuff here -------


       }

now whenever i want to check if the admin is logged in i simply do
{if $admin_logged)
more option for admin here
{/if}
Please if there is a better way to do it, please let me know, but I'm posting here in case anyone else is wondering.

#4
[eluser]Jagar[/eluser]
If you used http://ellislab.com/forums/viewthread/60050/ to install smarty in CI, or if you have a line like this somewhere for smarty:
Code:
//register Smarty resource_name 'ci'
        $this->register_resource('ci',array($this, 'ci_get_template', 'ci_get_timestamp','ci_get_secure','ci_get_trusted'));

then you are able to access most CI stuff from within smarty templates.

CI Session, you must either autoload session, or load it from the controller where it loads the view template.
Code:
{$CI->session->userdata('session_name')}

Access segment:
Code:
{$CI->uri->segment(2)}


You can access almost everything from smarty templates. If you get an error, be sure to load the helper or library.

I hope this will help somebody who is looking for something like that, like I did.

Jagar,

#5
[eluser]Bloupies[/eluser]
Hi,

I'm trying to do it but I don't know how ..

Someone can help me what to do exactly ?
(I want to access code igniter session in my smarty file )

#6
[eluser]Jagar[/eluser]
Did you tried my last reply? That's what I have done and it worked.

Regards

#7
[eluser]Bloupies[/eluser]
I don't have any smarty_parser file ..

I'm using this : https://github.com/cryode/CodeIgniter_Smarty

Edit : Resolved


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.