Parse html template in codeigniter

Rakesh Sharma      No Comments  

Parse html template in codeigniter using Template Parser Class

Parse html template in codeigniter

Sometimes we need to parse html data, like sending an email with html template. In codeigniter a library exist to handle this process. Let’s see how to parse html template in codeigniter. In codeigniter their is Template Parser Class, which handle html template parsing process. The Template Parser Class parses pseudo-variables contained within your view files. Also it can parse simple variables or variable tag pairs.

Pseudo-variables defined by curly brasises like.

{my_var}

So you need to add pseudo-variables in your html to parse html template in codeigniter. Let’s create an example of how to parse html template in codeigniter. Create a view file in application/view/my-template.php and add some pseudo-variables and html.

<html>
<head>

</head>
<body>
<h1>{my_msg_title}</h1>
<p>{my_msg_body}</p>
</body>
</html>

Now we need to replace these pseudo-variables to our variable values. So first we need initializing the Class.

$this->load->library('parser');

Now after initiate class we can use template parser class function $this->parser->parse() to parse html like.

//replacing pseudo-var to controller vars.
$body = array('my_title' => $my_title, 'my_msg_title' =>$my_msg_title, 'my_msg_body' => $my_msg_body);
//loding view file to parse function.
$messagebody = $this->parser->parse('my-template', $body, true);

Note: if you do want the data returned instead of sent to the output class you can pass TRUE (boolean) to the third parameter.

Variable Pairs

If you want to parse a pairs of variables, use your pseudo variables like

{my_var} data... {/my_var}

In this case, the entire chunk of data between these pairs would be repeated multiple times, corresponding to the number of rows in a result.
Parsing variable pairs is done using the identical code shown above to parse single variables, except, you will add a multi-dimensional array corresponding to your variable pair data. Let’s see an example.

<html>
<head>

</head>
<body>
<h1>{my_msg_title}</h1>
<p>{my_msg_body}</p>
<h1>{title}</h1>
<h2>{description}</h2>
<p>{/my_msg_body}</p>
</body>
</html>

let’s parse this html template:-

$this->load->library('parser');
$data = array('my_title' => $my_title, 
              'my_msg_title' =>$my_msg_title,
              'my_msg_body' => array(
                            array('title' => 'Title 1', 'description' => 'desc 1'),
                            array('title' => 'Title 2', 'description' => 'desc 2'),
                            array('title' => 'Title 3', 'description' => 'desc 3'),
                            array('title' => 'Title 4', 'description' => 'desc 4'),
                            array('title' => 'Title 5', 'description' => 'desc 5')
                          )
            );

$this->parser->parse('my-template', $data);

So like this you can parse bunch of variable pairs. These are examples how easy you can manage html template parsing in codeigniter. For more information their official doc section have good tutorials follow Template Parser Class.