1.6. A Slightly Different Approach: Templates

Along with standard PHP templates, CodeIgniter offers a lightweight template parser. To use the parser, all you have to do is load it into your controller and pass in the data from the model. The view would then use pseudo-variables instead of actual PHP variables, thus making your views free of PHP altogether. If you are familiar with SMARTY templates, you will see that these pseudo-variables and pseudo-structures align with your expectation of how the SMARTY world works. If you are new to this type of template parsing, you won't have any direct experience with it, but it's pretty easy to pick up. For example, instead of calling $title as you did before, you'd use {title}. Semantically, the structures and naming conventions are similar enough to the PHP variables and structures that you won't have much trouble following along.

One final note before continuing the discussion: There's normally no need to modify the model if you're going to work with the template parser. Why not? Well, it just so happens that the template parser will accept the data structures you give it. The only thing that changes is how you call out and use the data structure from the parsed template.

Using Third-Party Templating Systems

Most of the time, you'll want to use PHP templates with CodeIgniter, as it offers the fastest way to both develop and generate views. However, there might be some cases in which you'll want to use a third-party templating system, ...

Get Professional CodeIgniter® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.