Welcome Guest, Not a member yet? Register   Sign In
Yet Another Smarty Thread
#1

[eluser]xtramix[/eluser]
Integration of CI and Smarty has been discussed many times on this forum, yet I've decided to post here my own solution, in the hopes that someone might find it useful.

The only advantage this particular approach offers over other suggested solutions, is its ability to incorporate CI and Smarty code in the same template, rather than forcing you to choose between the two. If your templates contain pure Smarty code, you can load/parse them in a usual fashion, e.g.:
Code:
$this->smarty_parser->parse("template.tpl", $data);
However, if your template started out as a regular CI view, you can switch to Smarty at any time and still keep all the php code in your template intact, knowing that all CI function calls and variables will be processed correctly. Simply prefix the template name with "ci:" to tell Smarty it's a "resource", and it will ask CI to load and pre-parse the view before handing it off to Smarty engine:
Code:
$this->smarty_parser->parse("ci:template.tpl", $data);

The steps necessary to set up Smarty under CI in this case are very similar to those suggested by other authors:

1. Rename Smarty "libs" folder to "smarty", and move it to CI's application/libraries directory.

2. Create Smarty_parser.php file in the same directory and paste the following code:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

require "smarty/Smarty.class.php";

class Smarty_parser extends Smarty {

    function Smarty_parser($config = array())
    {
        parent::Smarty();
        
        if (count($config) > 0)
        {
            $this->initialize($config);
        }

        // register Smarty resource named "ci"
        $this->register_resource("ci", array($this,
                        "ci_get_template", "ci_get_timestamp", "ci_get_secure", "ci_get_trusted")
        );
                                      
        log_message('debug', "Smarty_parser Class Initialized");
    }

    /**
     * Initialize preferences
     */    
    function initialize($config = array())
    {
        foreach ($config as $key => $val)
        {
            if (isset($this->$key))
            {
                $method = 'set_'.$key;
                
                if (method_exists($this, $method))
                {
                    $this->$method($val);
                }
                else
                {
                    $this->$key = $val;
                }            
            }
        }
    }

    /**
     *  Set the left/right variable delimiters
     */
    function set_delimiters($l = '{', $r = '}')
    {
        $this->left_delimiter = $l;
        $this->right_delimiter = $r;
    }

    /**
     *  Parse a template using Smarty engine
     *
     * Parses pseudo-variables contained in the specified template,
     * replacing them with the data in the second param.
     * Allows CI and Smarty code to be combined in the same template
     * by prefixing template name with "ci:".
     */
    function parse($template, $data, $return = FALSE)
    {
        if ($template == '')
        {
            return FALSE;
        }

        $CI =& get_instance();
        
        $CI->benchmark->mark('smarty_parse_start');
        
        if (is_array($data))
        {
            $this->assign(&$data);
        }
        
        // make CI object directly accessible from a template (optional)
        $this->assign_by_ref('CI', $CI);
        
        $template = $this->fetch($template);

        if ($return == FALSE)
        {
            $CI->output->final_output = $template;
        }
        
        $CI->benchmark->mark('smarty_parse_end');
        
        return $template;
    }

    /**
     * Smarty resource accessor functions
     */    
    function ci_get_template ($tpl_name, &$tpl_source, &$smarty_obj)
    {
        $CI =& get_instance();
        
        // ask CI to fetch our template
        $tpl_source = $CI->load->view($tpl_name, $smarty_obj->get_template_vars(), true);
        return true;
    }
    
    function ci_get_timestamp($view, &$timestamp, &$smarty_obj)
    {
        $CI =& get_instance();
        
        // Taken verbatim from _ci_load (Loader.php, 580):
        $ext = pathinfo($view, PATHINFO_EXTENSION);
        $file = ($ext == '') ? $view.EXT : $view;
        $path = $CI->load->_ci_view_path.$file;
        
        // get file modification date
        $timestamp = filectime($path);
        return ($timestamp !== FALSE);
    }
    
    function ci_get_secure($tpl_name, &$smarty_obj)
    {
        // assume all templates are secure
        return true;
    }
    
    function ci_get_trusted($tpl_name, &$smarty_obj)
    {
        // not used for templates
    }
}
?>

3. Create Smarty_parser.php file in the application/config directory:
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

// Please see Smarty user guide for more info:
// http://smarty.php.net/manual/en/api.variables.php

// The name of the directory where templates are located.
$config['template_dir'] = dirname(FCPATH);

// The directory where compiled templates are located
$config['compile_dir'] = BASEPATH.'cache/';

//This tells Smarty whether or not to cache the output of the templates to the $cache_dir.
$config['caching']        = 0;

// This forces Smarty to (re)compile templates on every invocation.
// When deploying, change this value to 0
$config['force_compile'] = 1;
$config['compile_check'] = TRUE;
?>

4. In your controller, load Smarty library and parse the template, as such:
Code:
// uncomment the line below to load the library under the name "smarty", if desired
// $this->load->_ci_varmap['smarty_parser'] = 'smarty';
$this->load->library('smarty_parser');
$this->smarty_parser->parse("ci:template", $data);

Hope this helps.


Messages In This Thread
Yet Another Smarty Thread - by El Forum - 09-03-2007, 03:32 PM
Yet Another Smarty Thread - by El Forum - 09-03-2007, 05:16 PM
Yet Another Smarty Thread - by El Forum - 09-12-2007, 11:02 PM
Yet Another Smarty Thread - by El Forum - 09-13-2007, 03:41 AM
Yet Another Smarty Thread - by El Forum - 02-01-2008, 04:41 PM
Yet Another Smarty Thread - by El Forum - 02-03-2008, 05:08 PM
Yet Another Smarty Thread - by El Forum - 02-06-2008, 03:16 PM
Yet Another Smarty Thread - by El Forum - 02-25-2008, 09:11 AM
Yet Another Smarty Thread - by El Forum - 03-02-2008, 09:17 PM
Yet Another Smarty Thread - by El Forum - 03-04-2008, 12:14 AM
Yet Another Smarty Thread - by El Forum - 03-05-2008, 12:24 PM
Yet Another Smarty Thread - by El Forum - 03-06-2008, 02:04 PM
Yet Another Smarty Thread - by El Forum - 03-07-2008, 03:05 PM
Yet Another Smarty Thread - by El Forum - 03-10-2008, 08:18 PM
Yet Another Smarty Thread - by El Forum - 06-12-2008, 06:29 AM
Yet Another Smarty Thread - by El Forum - 07-01-2008, 12:26 AM
Yet Another Smarty Thread - by El Forum - 08-21-2008, 03:48 AM
Yet Another Smarty Thread - by El Forum - 02-02-2009, 09:14 AM
Yet Another Smarty Thread - by El Forum - 02-02-2009, 09:18 AM
Yet Another Smarty Thread - by El Forum - 04-23-2009, 08:19 AM
Yet Another Smarty Thread - by El Forum - 04-23-2009, 08:24 AM
Yet Another Smarty Thread - by El Forum - 04-23-2009, 08:28 AM
Yet Another Smarty Thread - by El Forum - 04-23-2009, 08:44 AM
Yet Another Smarty Thread - by El Forum - 08-31-2009, 04:08 AM
Yet Another Smarty Thread - by El Forum - 08-31-2009, 06:05 AM
Yet Another Smarty Thread - by El Forum - 08-31-2009, 10:34 PM
Yet Another Smarty Thread - by El Forum - 08-31-2009, 10:48 PM
Yet Another Smarty Thread - by El Forum - 08-31-2009, 11:20 PM
Yet Another Smarty Thread - by El Forum - 09-27-2009, 09:33 AM
Yet Another Smarty Thread - by El Forum - 05-10-2010, 05:02 AM
Yet Another Smarty Thread - by El Forum - 05-10-2010, 08:38 AM
Yet Another Smarty Thread - by El Forum - 05-10-2010, 06:57 PM
Yet Another Smarty Thread - by El Forum - 05-17-2010, 11:05 PM
Yet Another Smarty Thread - by El Forum - 11-21-2010, 10:31 PM
Yet Another Smarty Thread - by El Forum - 03-25-2011, 12:58 AM
Yet Another Smarty Thread - by El Forum - 05-02-2011, 10:34 AM
Yet Another Smarty Thread - by El Forum - 07-03-2011, 07:26 AM
Yet Another Smarty Thread - by El Forum - 07-03-2011, 08:57 AM
Yet Another Smarty Thread - by El Forum - 02-17-2012, 11:12 AM
Yet Another Smarty Thread - by El Forum - 02-17-2012, 11:08 PM



Theme © iAndrew 2016 - Forum software by © MyBB