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 coolphptools.com 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 coolphptools.com. 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:
<snippet>
[~ if $message ~] Please complete the form. [~ /if ~]
</snippet>
… 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: https://github.com/iarfhlaith/Code-Igniter-and-Smarty
Posted by Iarfhlaith |
Link
|
Share |
6 Comments »





