CI Sessions and Smarty

#1
[eluser]Unknown[/eluser]
Hi All,

Since you are reading this, I suppose you're looking for a fancy solution to implement the famous (or infamous) CI sessions into your Smarty template parser.

Well, after reading a couple of posts about how other users have implemented this. I decided to create my very own solution.
For starters, my application/libraries directory is very clean. It only has the s.php classfile which implements smarty into the CI framework, and the folder "smarty" for all the smarty-internal-files.

My s.php file looks like this:
Code:
<?php
require_once "smarty/Smarty.class.php";
class S extends Smarty {
    function S() {
        $this->Smarty();

     //load configs
        $config =& get_config();

        $this->template_dir = (!empty($config['smarty_template_dir']) ? $config['smarty_template_dir'] : BASEPATH . 'application/views/');
        $this->compile_dir = (!empty($config['smarty_compile_dir']) ? $config['compile_dir'] : BASEPATH . 'cache/');

        $this->clear_all_cache();
    }

    function display($resource_name) {
        // add .tpl if it hasn't already been done
        if (strpos($resource_name, '.tpl') === false) {
            $resource_name .= '.tpl';
        }
        // pass the resource to the parent method
        return parent::display($resource_name);
    }
}
?>
This code is in all implementations very similar. There are some variations, but I find this one to be the easiest of all without hurting smarty syntax. Don't forget to autoload the smarty class by adding the 's' to the list of libraries autoloaded.
Code:
$autoload['libraries'] = array('database', 'session', 's');

To take my CI session object, and assign it to a template, I used a 'hook'. If you're not familiar with hooks, please check the user_guide.

To enable hooks, set $config['enable_hooks'] = TRUE; in config/config.php

To add a hook, modify config/hooks.php and add the following:
Code:
$hook['post_controller'][] = array(
    'class' => 'Smartyhook',
    'function' => 'assignSession',
    'filename' => 'smartyhook.php',
    'filepath' => 'hooks',
    'params' => array()
);
This hook will be launched after your method has been called. If you call the hook before your method has been called, it will still work, but won't be showing the sessiondata you added during the method (which is quite inconveniënt for let's say, a login routine).

Now, let's add the code for the hook (hooks/smartyhook.php):
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Smartyhook {
    function assignSession() {
        $CI =& get_instance();
                $CI->s->assign('session',$CI->session->userdata);
                //template override from session
        if($CI->session->userdata('template')) {
            $CI->s->display($CI->session->userdata('template'));    
        } else {
            $CI->s->display('default');    
        }
    }
}

?>
You may wonder what the last section is doing. Well, since you can't first display() and then assign() a variable to a template, the display() method must be called after the session is assigned to the template.

The advantage of this method is: you never have to call the default template to display in your methods. And, if you want to display another template, you just assign the name to your session.

So, this is my solution. For every project a slight modification might be needed, but in general, this method works for me. Hope you'll like it.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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