Subscribe

RSS Feed (xml)

How to Integrate Smarty Template to Code Igniter and to use as a View

Introduction


In this tutorial I will show you how to integrate Smarty Template in to Code Igniter and used it as view.

Requirements


1. Code Igniter
2. Smarty Template

Installation and Instructions


1. Download, unzip and install the Code Igniter Framework.

2. Download and unzip the Smarty Template

2.1 Locate the ‘libs’ directory in your Smarty directory ‘Smarty-*.*.**’

2.2 Copy the ‘libs’ directory to Code Igniter installation
[code igniter installation]/system/application/libraries/

2.3 Create a directory 'smarty' in Code Igniter
[code igniter installation]/system/application/libraries/

2.4 Rename the 'libs' to 'libs_*.*.**' (Ex. libs_2.6.22) *.*.** is the Smarty version
and place it to [code igniter installation]/system/application/libraries/smarty/

You can easily manage your Smarty version using the code below.

require "smarty/libs_2.6.22/Smarty.class.php";

3. Create the file below and name it to 'mysmarty.php' and place it to
[code igniter installation]/system/application/libraries/

File: [code igniter installation]/system/application/libraries/mysmarty.php
 ?php if (!defined('BASEPATH')) exit('No direct script access allowed');

 require "smarty/libs_2.6.22/Smarty.class.php";

 class Mysmarty extends Smarty
 {
  public function __construct ( )
  {
   parent::Smarty( );

   $config =& get_config( );   

   $this->left_delimiter =  '{{';
   $this->right_delimiter =  '}}';   

   // absolute path prevents "template not found" errors
   $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/'); //use CI's cache folder
  }

  public function view ( $resource_name, $cache_id = null )
  {
   if ( strpos($resource_name, '.') === false )
    $resource_name .= '.tpl';

   return parent::display( $resource_name, $cache_id );
  }
 }

?

4. Edit your 'autoload.php' and autoload your 'mysmarty'
[code igniter installation]/system/application/config/autoload.php

See the code below

$autoload['libraries'] = array( 'database', 'session', 'mysmarty' );

IMPORTANT: If you are using 'mysmarty' in other library, it must be loaded first prior to your other plugin.

Correct:

$autoload['libraries'] = array( 'database', 'session', 'mysmarty', 'otherlibrary' );


Congratulation! You have successfully integrated the Smarty Template to your Code Igniter Framework. Now, the next step is how to use Smarty to your template.

Your templates must reside in
[code igniter installation]/system/application/views/


View:
[code igniter installation]/system/application/views/home.tpl

....
< body >
     {{* display value *}}
     {{$my_template_variable}}
< /body >
....


Controller:
[code igniter installation]/system/application/controller/home.php

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

 class Home extends Controller
 {
  function __construct ( )
  {
   parent::Controller( );
  }  

  public function index ( )
  {
   // set value ...
            $this->assign( 'my_template_variable', 'This is the value.' );

   // call the template ...
            $this->mysmarty->view( 'home' );
  }
 }

?


Now, try it! don't forget to leave comment ...

0 comments:

Related Posts with Thumbnails