If you are not familiar with CodeIgniter, it is a lightweight Model-View-Controller (MVC) framework written in PHP. It is an open source project maintained by EllisLabs. CodeIgniter has a templating system built in but, the last time I looked at it, it does not have many features. It also implements the templating by filtering the page using preg_match() and string replacement. For web sites with low traffic volumes and few variable replacements, this approach is fine. As the traffic volume picks up or with larger pages, this may impact the response time of the web site.
Smarty is another open source project that handles templates and is also written in PHP. It has been around for a while and has many features beyond the simple templating engine built into CodeIgniter. Smarty also “compiles” templates into pure PHP which makes them slightly faster to execute.
In this article you will learn how to install and use Smarty as a template engine with Code Igniter Framework.
Codeigniter is an awesome framework, it’s idea of a parser library that ships with it is not. I have written a simple library that extends Codeigniter’s native view loading to allow you to use Smarty 3 in your Codeigniter projects to render your views.
Codeigniter 2.0 as this library uses the core and third_party folders, although with some changes it will work with 1.7.2 / 1.7.3 versions of Codeigniter.
- Extends native Codeigniter view loading so you can still use $this->load->view() to load your templates. This also means that if your Codeigniter application is using $this->load->view() you won’t need to change any of your code to use this library.
- Allows you to use Smarty specific features including template inheritance inside of your views.
- Uses your views directory for loading templates but the location of views can be changed if you don’t use the standard CI views folder.
Really helpful thread on codeigniter forums that can help you.
In this tutorial I will show you how to integrate Smarty Template in to Code Igniter and used it as view.
6. CodeIgniter: Smarty as a full-fledged template engine
I really like the many libraries and helpers available, they make the many tiresome things (forms, pagination, input validation) that are rehashed every time a new web application is written a joy, since this is all done for you already. Not that I would recommend someone new to web development to CI, since I’m an advocate of the fact that you must go through some pain to appreciate the beauty in things. But then I digress.
I’m not so keen about the template parsing part though. I had almost expected something like Smarty, but guess I was spoilt. I felt that mixing PHP/HTML syntax was simply… ugly. Tsk, tsk. The user guide indicates that it was an intentional decision to not include a full-blown templating solution, and I think CI suffers for that fact.