If you like to use smarty for designing your views while developing codeigniter applications, just read on. Smarty is a powerful templating engine for php. Its template language is simple yet powerful. By default, codeigniter doesn't use any templating system. Instead it relies on plain old php for designing views. Integrating smarty into codeigniter is rather simple. First, download the latest smarty version and copy the libs folder into /system/application/libraries/.

Now create a new file “View.php” inside the /system/application/libraries folder. Note: the case of the filename is important it must be “View.php”. Now put the following code inside the View.php file.

<?php

require_once(dirname(__FILE__).'/libs/Smarty.class.php');

class View extends Smarty{

public $layout='default';

function __construct(){

parent::__construct();

$this->compile_check = true;

$this->debugging = false;

$this->template_dir = APPPATH.'views';

$this->compile_dir = APPPATH.'temp';

}

function show($name){

$content=$this->fetch($name.'.tpl');

$this->assign('content',$content);

$this->display('layout/'.$this->layout.'.tpl');

  }

}

?>

 

Point of interest:

We have a member variable on our view class which will hold the name of the layout template. By default it is set to ‘default’.

In the constructor, we are telling smarty where to look for our view templates and where to save the compiled version of our templates. Smarty will look for views in /system/application/views/ and it will save the compiled templates inside the /system/application/temp/ folder. Please do set the required permissions on the /system/application/temp/ folder enabling Smarty to write to it.

We will be calling the show() function of our view class for rendering the views. The $name parameter should be the path to the view without the “.tpl” extension. For example:

$this->view->show(‘site/login’);

Inside the show () function, we are fetching the output of the view specified using the following line

$content=$this->fetch($name.'.tpl');

Then, we are assigning the output of the view to a smarty variable named ‘content’

$this->assign('content',$content);

Finally, we are displaying the layout template using the below line

$this->display('layout/'.$this->layout.'.tpl');

Now our layout template should use the assigned variable ‘content’ for outputting the contents of the view captured earlier. For example

<html>

<head>…</head>

<body>

{$content}

</body>

</html>

Neat! Instead of views including the headers and footers they need, now we have a common, site wide layout template much like ASP.NET’s master pages. The layout template must be placed inside a folder named “layout” inside the /system/application/views/ folder.

Using the view class

For using this new view class, you must load the View library using

$this->load->library(‘view’);

Or you can auto load the view library so that you do not have to load it in each controller action. Once the view class is loaded either through auto loading or using code, use the similar code as shown below for displaying views.

$this->view->assign('name’,’nirandas’);

$this->view->show('hello’);

All methods of the Smarty object are available to us through $this->view variable. For assigning variables we can use

$this->view->assign(‘var_name’,’var_value’);

And for displaying our pages we can use

$this->view->show(‘view_name’);

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList