January 18, 2013 CodeIgniter Image

Codeigniter Template Parser Class

CodeIgniter Template Parser Class

When we talk about template parser, we basically refer to a template engine, that let’s you seperate your logic with the design. So the real and most obvious benefit of using a template parser would be to have both designer and developer work at the same time during the same project. Now, many of you might say that, this benefit is already visible with the MVC approach that CodeIgniter already gives us, but let’s look a little deeper in, and see how this can be useful for everyone. Remember though, this CodeIgniter template parser class is very basic, and this post is aiming for those who do not know what a parser is. At the end of this post, I will give a few links to known template libraries out there, that can be more powerful.

For your reference, here is how EllisLabs describes the CodeIgniter Template Parser Class:

CodeIgniter Template Parser Class enables you to parse pseudo-variables contained within your view files. It can parse simple variables or variable tag pairs.

With this being said, if any of you have ever heard of Smarty template engine, or have ever worked with it, then it should be a breeze to understand. Let’s take a look at a very simple example. Let’s say we work for social network ‘X’, and the designer does not feel comfortable using files with PHP on it. An easy way to make this transition would be to have some kind of templating engine. That way, the designers will not be afraid of breaking essential code for any of the scripts, and well on their way to modifying the websites design through their CSS files. In our example, we will assume that we have our view for a profile page. This view can be very generic and let’s see how:


As you can see, instead of having actual PHP code in the view file, all we have is some kind of template with the pseudo variables. Anything between curly braces are to be considered as these pseudo variables that the template parser class will replace.

How are pseudo variables replaced?

So you might ask yourself, how do you use this template parser class, and how does it know what to replace. Well, the first thing you have to do is initialize the class in order for you to be able to use it. To initialize it, you do it like you would any other library :

Next, you would have to name the variables and send them to the parser to be (of course) parsed. This can be done in the same way you would prepare your data within the controller, receiving it from the models and sending them to the views, except, in this case, we would send them to the parser with the use of {php}$this->parser->parse();{/php}. This method accepts 2 parameters, which will be, the view, and the data variables. I would recommend placing these variables inside an array, so that it looks cleaner.

Let’s take a look at the example profile class, sending the data to the profile_view.php file.

Note how the first parameter has the view file name, and the second parameter has the data that will be passed into the view. This works exactly like {php}$this->load->view($view_file);{/php}, so expect the output to be in the browser. Also, note how the wall messages are being repeated. This is done by opening and closing the same pseudo variable.

Now, even though codeigniter has this template parser class, it does not mean that it is the best for the framework. There are other template parsers which can work better than this one. The following is a list of CodeIgniter Templates parser class for you to view and explore:

Phil Sturgeon’s CodeIgniter Template.


Template Library for CodeIgniter

Comper Template Parser


If anyone finds more, please do let me know so that I can add to the list.

2 thoughts on “Codeigniter Template Parser Class

  1. Hi there! This blog post could not be written any better! Looking through this post reminds me of my previous roommate!

    He always kept talking about this. I will send this article to him.
    Pretty sure he’ll have a good read. Thank you for sharing!

Leave a Reply

Your email address will not be published. Required fields are marked *

The qTranslate Editor has disabled itself because it hasn't been tested with your Wordpress version yet. This is done to prevent Wordpress from malfunctioning. You can reenable it by clicking here (may cause data loss! Use at own risk!). To remove this message permanently, please update qTranslate to the corresponding version.