I’m a newly converted fan of the CodeIgniter (CI) framework for PHP. I was introduced to it a few weeks back, and there’s been no looking back. PHP has suffered much critique of late here, here and here, but if there’s a star example of beautiful PHP code, here’s it. It helps developers write code better, period.
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.
So… why not combine CI and Smarty? Note that this is not a CI or Smarty tutorial, if you need help on those things, read the manual! :) According to CI convention, I’ve put the Smarty source code into the libraries folder. Since there are quite a number files, I put it in a subfolder “smarty”, so the path to Smarty is:
Now, we need to extend the Smarty (we’ll call it smarty_library), the code which we stored as:
There are plenty of Smarty configurations to play around with, but we’ll stick to just two (template_dir and compile_dir). Using absolute paths help prevent those pesky “template not found” errors.
// absolute path prevents 'template not found' errors
$this->template_dir = '/path/to/CodeIgniter/system/application/views/'; // use CI's views folder
$this->compile_dir = '/path/to/CodeIgniter/system/cache/'; //use CI's cache folder -- chmod 777 cache required
// URL helper required
$this->assign('site_url', site_url()); // so we can get the full path to CI easily
} // END class smarty_library
Now that this is done, you can put the library load call in your CI application as:
Access your Smarty object at:
Please drop comments! I’d love to hear from other like-minded developers! :)