CodeIgniter Template Parser Class
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:
profile_view.php
PHP
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<link type="text/css" href="http://staging.athletescentral.com/css/buttons.css" rel="stylesheet">
<title>{user_name} {separator} {profile_title}</title>
</head>
<body>
<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>
<ul>
<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>
</ul>
</div>
</div>
</div>
<h3>{wall_heading}</h3>
{wall_entries}
<h5>{user_name}</h5>
<div class="wall_entries_body">{body}</div>
{/wall_entries}
</div>
</body>
</html>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<link type="text/css" href="http://staging.athletescentral.com/css/buttons.css" rel="stylesheet">
<title>{user_name} {separator} {profile_title}</title>
</head>
<body>
<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>
<ul>
<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>
</ul>
</div>
</div>
</div>
<h3>{wall_heading}</h3>
{wall_entries}
<h5>{user_name}</h5>
<div class="wall_entries_body">{body}</div>
{/wall_entries}
</div>
</body>
</html>
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 :
PHP
<?php
class Profile extends CI_Controller{
public function __construct(){
// Load parent construct
parent::__construct();
// Load parser library
$this->load->library('parser');
}
}
?>
<?php
class Profile extends CI_Controller{
public function __construct(){
// Load parent construct
parent::__construct();
// Load parser library
$this->load->library('parser');
}
}
?>
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.
PHP
<?php
class Profile extends CI_Controller{
public function __construct(){
// Load parent construct
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'] = 'www.yourdomain.com/';
$data['home_header_text'] = 'Home Page';
// Get the user information from the model
// First load model
$this->load->model('user_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 );
}
}
?>
<?php
class Profile extends CI_Controller{
public function __construct(){
// Load parent construct
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'] = 'www.yourdomain.com/';
$data['home_header_text'] = 'Home Page';
// Get the user information from the model
// First load model
$this->load->model('user_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 {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.