Code Igniter and Smarty Template Engine

Earlier today I decided to spend some time looking at new ways to improve Code Igniter’s template code. I’ve been using Code Igniter for about a year now and I’ve always been a little frustrated with how it’s template code can get a little out of hand. Sure, PHP has a shorthand syntax but it can still get a little messy without a lot of self discipline. Now I could blame myself and my sometimes undisciplined coding habits, but I’d rather blame my tools and look for a better way, which is why we aren’t all still coding in the 8086 assembly language.

So since then, I’ve been on the constant lookout for better ways to structure the template code. I’ve been coding professionally now for a good 10 years and I’ve still not seen a better way to seperate out PHP code from frontend code than by using Smarty.

I started using Smarty in 2005. It’s the most popular PHP template engine and sports it’s own template language. If you’re a PHP developer and you’ve never checked it out, you should take a look. You’ll have to go through the hassle of learning it’s own templating language, but once you’ve done it, you’ll see how much simpler your frontend code can be. In fact, I’ve never managed to find a better way to write it, which is why I started looking for ways to improve Code Igniter’s frontend code, and you’ll never guess what I found. It turns out that I can have my cake and eat it, I can use Smarty with Code Igniter!

I stumbled upon this excellent tutorial, written by where it shows step by step exactly how you can get the two of them working together. So, I followed it, and made some small improvements along the way.

I won’t repeat what the tutorial covers, you can check it out if you want to go through the steps yourself, but if you just want to try it out, then you can download my working copy of Code Igniter and Smarty.

I used Code Igniter version. 2.0.3 and Smarty Template Engine version 3.1.4 along with the bridging code provided on In my environment I used PHP version 5.3.5 running on Apache 2.2.17. It’s also worth noting thatย  Smarty v.3 is not backwards compatible with PHP 4.x so if you haven’t updated to PHP 5.x by now, you should.

I made two slight amendments to the steps outlined in the tutorial.

Template Code Delimiters

I changed the left and right template code delimiters from the standard curly brackets to a combination of tildes and square brackets. If you write a lot of Javascript and embed it in the template files then the default code delimiters in Smarty can cause a few issues…

So with my new change {smarty_variable} becomes [~smarty_variable~].

It’s a little more verbose, but gets around having to use {literal} tags everytime you need to add Javascript in your .tpl files.

Error Reporting Level

By default in Smarty 3, template error level reporting is not used. Instead Smarty uses the global error reporting defined by PHP. As many Smarty templates use code such as:

[~ if $message ~] Please complete the form. [~ /if ~]

… then in times when $message is not defined and PHP is set to throw an error on a Notice then this
code will display an error. As this code is very common, particularly in legacy .tpl files, I amended
the Smarty.php file again to not throw notices on errors in the Smarty templates.

If you haven’t tried out this combination, then this should help you get started easily. It’s a great combination that combines the power and structure of Code Igniter with the simplicity of Smarty in the view.

Download the Code Igniter and Smarty Package.

UPDATE: I’ve put the code up on Github:


6 Comments added. Add comment?

  1. Michael Flanagan says:

    Iarfhlaith, have you had a look at CodeIgniter Sparks? — — I believe it’s being brought into the core in a future CodeIgniter update. Easy to setup and get going as it is.

    I remember I’d seen this before (don’t use Smarty myself), the Smarty spark:

    Nov 4, 2011
  2. Iarfhlaith says:

    Hey Michael, I hadn’t heard of Sparks, it looks great. A bit like PEAR but much leaner.

    The Smarty spark looks great too, I’ll definitely check that one out. After reading through the How-To it looks like it’s implemented in a slightly different way, but looks terrific all the same.

    Thanks for sharing ๐Ÿ™‚

    Nov 4, 2011
  3. Shairyar says:


    When trying to debug the console opens up in same window, can you tell me how to have a pop-up console rather than it opening up in same window.

    Jan 8, 2012
  4. Iarfhlaith says:

    Hey Shairyar,

    I’m presuming you mean the Smarty debug console?

    Here’s the official documentation on the Debugging Console:

    Essentially you need to set $debugging to true in Smarty. And have it run using display().

    Bare in mind, what I’ve done here is very simple, and I haven’t made any significant changes to the core libraries.

    Jan 8, 2012
  5. Shairyar says:


    Thanks for getting back, what i meant was when i turn on the debugging by using {debug} in .tpl file the console does not popup, it just opens up in the browser window where my website is being displayed. i have to keep turning debug off by deleting the {debug} from .tpl file in order to view my website and continue my work, i hope it makes sense what i am saying.

    So have you changed anything to the way debug console should popup as it is really a pain trying to debug.

    Jan 8, 2012
  6. Iarfhlaith says:

    That’s odd alright. I used the latest stable copy of Smarty and haven’t changed the debug console code at all.

    Best to go to the Smarty Forums and ask Mohrt. He’s the guy you want to ask ๐Ÿ™‚

    Jan 9, 2012

Sorry, comments for this entry are closed at this time.