CodeIgniter + Twig = Quick n Dirty ™

Categories Web Dev

In full disclosure, I’m not completely new to CodeIgniter, having built a few freelance projects using the 2.x framework in the past due to it’s speed, flexibility and well-rounded core. It provides a great jumping-off spot for basic PHP-based projects that can benefit from a MVC framework without all the cruft you find in things like CakePHP, Laravel, and Symfony. Another selling point is that in several framework shootouts, CodeIgniter consistently comes in with faster non-cached response times.

Interesting bit of news on a tangent, apparently it is now even more of a community-driven project since October 6, 2014, when the British Columbia Institute of Technology took over stewardship from Ellis Lab, the creator of the framework (and the underpinnings for its ExpressionEngine CMS).

OK, back to CodeIgniter and templates… I was recently working on a little side project that the maintainers wanted to be able to manage all CI templates, but they were not very PHP-savvy. This kind of ruled out the core CI view templates as they are pretty much eval’d PHP files with global variables defined by the calling code. I started off using the built-in template library provided with CI, but quickly realized it was providing nothing more than variable substitution. What I really needed was something that could also provide basic control logic as well, but not require someone to know PHP. I also wanted to use something widely adopted in the web dev community in order to give the project some longevity. What I settled on was Twig.

Twig is the template engine used by Symfony (yeah, I fully realize I dissed that framework a tad earlier) and provides a rich set of template features which can be used for something as simple as variable substitution to something as complex as template inheritance. The nice thing is it’s written purely in PHP and can be used standalone outside of Symfony. After some quick research, I discovered that Bennet Matschullat had already implemented a library for CodeIgniter 2.x to provide full integration with Twig. Happy dance!

To use it, download and extract the GitHub project into your CodeIgniter docroot (it places files in the correct places under the application folder) and download the latest Twig and extract it under application/third-party so the parent directory for the library is named Twig. Then in your controller output function, instead of invoking $this->load->view() or $this->parser->parse(), add the following lines:

That’s it!  The library will invoke the template, cache the compiled version, and return the output from Twig via the normal CodeIgniter output object used by the core view and template mechanisms.  Nice!

  • laynebay

    Thank for this post. I’ve become a huge fan of CI + Twig. A breath of fresh air. I’ve overcome a few hurdles, so if you have any questions, let me know.

    Thanks.

    • labbams4u

      Hi, now I am working on a project which uses twig+CI. In normal CI pagination, we just create the pagination link and send it to view page and shows it to user. But here with twig, how to pass the created html(pagination link html) to twig templates and show?
      Could you please explain? My email id : mslabba@hotmail.com

  • Andrea Manconi

    i think that edmundask (@edmundask ) did a better job. look here: https://github.com/edmundask/codeigniter-twiggy

    • arpieb

      Thanks for sharing that other Twig integration – somehow that one never showed up in my searches… :-