Welcome Guest, Not a member yet? Register   Sign In
Integrate Smarty to CI
#1

[eluser]Unknown[/eluser]
This can be useful to all who wants to use smarty templates in your ci applications.

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

require APPPATH .'libraries/Smarty-2.6.20/libs/Smarty.class'.EXT;

/**
* @file system/application/libraries/Mysmarty.php
*/
class CI_Smarty extends Smarty
{
    function CI_Smarty()
    {
        $this->Smarty();

        $this->ci =& get_instance();  
        $this->ci->config->load('smarty');
        $config =& get_config();
        
        // absolute path prevents "template not found" errors
        $this->template_dir = (!empty($config['smarty']['template_dir']) ? $config['smarty']['template_dir']  : APPPATH . 'templates/');
        $this->compile_dir  = (!empty($config['smarty']['compile_dir']) ? $config['smarty']['compile_dir'] : BASEPATH . 'cache/templates_c/'); //use CI's cache folder        
        $this->cache_dir  = (!empty($config['smarty']['cache_dir']) ? $config['smarty']['cache_dir'] : BASEPATH . 'cache/pages/'); //use CI's cache folder        
        $this->config_dir  = (!empty($config['smarty']['config_dir']) ? $config['smarty']['config_dir'] : APPPATH . 'config/'); //use CI's cache folder        
        $this->plugins_dir[] = APPPATH. 'plugins'; //My Plugins

        $this->debugging = $config['smarty']['debugging'];
        
        //URL helper required
        parent::assign('url', $config['base_url']); // so we can get the full path to CI easily
        parent::assign_by_ref('this', $this->ci); //CI instance          
    }
    
    function view($resource_name, $cache_id = null)   {
        if (strpos($resource_name, '.') === false) $resource_name .= '.tpl';
        return parent::display($resource_name, $cache_id);
    }
    
} // END class smarty_library

Smarty config
Code:
//Smarty
$config['smarty']['compile_check'] = true;
$config['smarty']['caching']       = false;
$config['smarty']['debugging']     = false;

//$config['smarty']['template_dir'], $config['smarty']['compile_dir']


Examples
Code:
<?php

class Example extends Controller {

    
  function Example ()
  {  
      parent::Controller();    
        
      $this->load->library('smarty');

      $this->smarty->assign('owner', 'Me'); //or any smarty method you can use

      require_once $this->smarty->_get_plugin_filepath('modifier', 'date_left');        
      $date_left = smarty_modifier_date_left('2009-04-03 13:24:00');
  }
}
#2

[eluser]Hasti[/eluser]
Hi

Can you give me the full example for smarty integration in CI

Waiting for your reply.
#3

[eluser]xwero[/eluser]
you don't have to load the config file in the library. CI load the config file for you if it has the same name as the class you want to load. To get the values from the file you add a params parameter to the constuctor of the library and you use that parameter.

I would put the base_url in the smarty config file and instead of adding the CI instance to the assign_by_ref method i would put a framework setting in the config file and use that to call the framework instance.
Code:
Smarty_Container($params)
{
   //...
   parent::assign_by_ref('this', $this->{'instance-'.strtolower($params['framework'])}());
   //...
}

function instance_ci()
{
   $instance =& get_instance();
   return $instance;
}

I would call it Smarty_container because that is the proper name IMO. If you use the third parameter of the load->library method you can attach the library as smarty if you want.

This makes the smarty container framework independent.




Theme © iAndrew 2016 - Forum software by © MyBB