Click here to Skip to main content
13,549,436 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 22 Jan 2013
Licenced CPOL

Codeigniter Template Parser Class

, 22 Jan 2013
Rate this:
Please Sign up or sign in to vote.
CodeIgniter Template Parser Class enables you to parse pseudo-variables contained within your view files. It can parse simple variables or variable tag pairs.

CodeIgniter Template Parser Class

When we talk about template parser, we basically refer to a template engine, that lets you separate 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 aimed at 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:


<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 

<html xmlns='' xml:lang='en' lang='en'>
<link type="text/css" 


<title>{user_name} {separator} {profile_title}</title>
    <div id="content">
        <div class="header">
        <a href="{home_url}"><h1>{home_header_text}</h1>
        </a> </div>
        <div class="line"></div>
        <div class="col_control_panel">    
            <div class="control_panel_header">
            <a href="{profile_url}">{profile_name}</a></div>    
            <div class="default_pic">
            <a href="{default_picture_view}"><img src="{default_pic_src}"></a></div>             
            <div class="homepanel">
                <div class="control_panel_view">        
                    <div class="control_panel_header">{user_control_panel}</div>                
                        <li><a class="cp_button" href="link1">link1 text</a></li> 
                        <li><a class="cp_button" href="link2">link2 text</a></li> 
                        <li><a class="cp_button" href="link3">link3 text</a></li> 
                        <li><a class="cp_button" href="link4">link4 text</a></li>
        <div class="wall_entries_body">{body}</div>

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 :

class Profile extends CI_Controller{
    public function __construct(){    
        // Load parent construct
        // Load parser 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:


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, send in the data to the profile_view.php file.

class Profile extends CI_Controller{
    public function __construct(){    
        // Load parent construct
        // Load parser library
        $this->load->library( 'parser' );
    public index( $user_name ){
        $data = array();
        $data['user_name'] = $user_name;
        $data['separator'] = '|';
        $data['profile_title'] = 'Profile view';
        $data['home_url'] = '';
        $data['home_header_text'] = 'Home Page';
        // Get the user information from the model
        // First load model
        // Grab user information
        $user_info = $this->user_model->get_user_info( $user_name );
        $data['profile_name'] = $user_info['first_name'].' '.$user_info['last_name'];
        $data['default_picture_view'] = $user_info['default_picture_view'];
        $data['default_pic_src'] = $user_info['default_pic_src'];
        $data['user_control_panel'] = 'Top Control Panel Menu';
        $data['wall_heading'] = 'Friends love =)';
        // Get wall entries
        // This model should return all messageson this users wall
        // including username and message body
        $data['wall_entries'] = $this->user->get_wall_posts( $user_name );    

        // After all data has been found, parse
        $this->parser->parse( 'profile_view', $data );        

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...

$this->load->view($view_file); 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:

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

The post Codeigniter Template Parser Class appeared first on Jotorres Web Development.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Puerto Rico Puerto Rico
As a computer scientist, I strive to learn more everyday in my field. Everything I learn, I share with my community by writing articles for future references.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180515.1 | Last Updated 22 Jan 2013
Article Copyright 2013 by jotorres
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid