Tutorialsplane

Codeigniter Template Parser Class Library


Codeigniter Template Parser Class Library – We can load template parser class library like this $this->load->library(‘parser’);. This library provide various functions that are used to perform simple text for pseudo-variable contained within your view files. It can parse simple variable or variable tags pair. Here in this tutorial, we are going to explain how to use template parser class library.


Codeigniter template parser class library

Let us understand how template parser class library works in codeigniter with examples.

Load template parser class library

First load template parser class library to use its functions, this library can be loaded simply as below-

How to load template parser class library:

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

Functions:-

There are following functions available in template parser class library. Now we will explain one by one.

1. Parsing templates.

Here is simple demo of Parsing templates.

First we have to create view page .

View Page Code [Folder Name : parser_view.php ]


        
                <title>{blog_title}</title>
        
        
                <h3>{blog_heading}</h3>

        {blog_entries}
                <h5>{title}</h5>
                <p>{body}</p>
        {/blog_entries}

        

Controller Code [Folder Name : parser_controller.php ]

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class parser_controller extends CI_Controller 
{
public function parser()
{
$this-?>load->library('parser');
$data = array(
'blog_title' => 'My Blog Title',
'blog_heading' => 'My Blog Heading');
echo $this->parser->parse('parser_view', $data, true);
}
}
?>

2. Variable Pairs.

Here is simple demo of Variable Pairs.

Controller Code

Controller Code [Folder Name : parser_controller.php ]

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class parser_controller extends CI_Controller 
{
public function parservariablepair()
{
$this-?>load->library('parser');
$data = array('blog_title'   => 'My Blog Title',
'blog_heading' => 'My First Website',
'blog_entries' => array(
array('title' => 'My Blog', 'body' => 'Sonu Kumar'),
array('title' => 'Contact Us', 'body' => 'sonukr321@gmail.com'),
array('title' => 'Company name', 'body' => 'SolidCoupon')));
$this->parser->parse('library/parser_view', $data);
}
}
?>

3. Usage Notes.

Here is simple demo of Usage Notes.

Controller Code

Controller Code [Folder Name : parser_controller.php ]

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class parser_controller extends CI_Controller 
{
public function note()
{
$this-?>load->library('parser');
$template = 'Hello, {firstname} {initials} {lastname}';
$data = array('title' => 'Mr','firstname' => 'John',
'lastname' => 'Doe');
$this->parser->parse_string($template, $data);
$template = 'Hello, {firstname} {lastname} ({degrees}{degree} {/degrees})';
$data = array('degree' => 'Mr','firstname' => 'John','lastname' => 'Doe',
'degrees' => array(array('degree' => 'BSc'),array('degree' => 'PhD')));
$this->parser->parse_string($template, $data);
}
}
?>

4. View Fragments.

Here is simple demo of view fragments.

Controller Code

Controller Code [Folder Name : parser_controller.php ]

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class parser_controller extends CI_Controller 
{
public function fregment()
{
$this-?>load->library('parser');
$template = '
    {menuitems}
  • {title}
  • {/menuitems}
'; echo "

Tutorialsplane

"; $data = array('menuitems' => array( array('title' => 'PHP', 'link' => '/php_page'), array('title' => 'CODEIGNITER', 'link' => '/codeigniter_page'), array('title' => 'CAKE PHP', 'link' => '/cakephp_page'), array('title' => 'BOOTSTARP', 'link' => '/bootstarp_page'))); $this->parser->parse_string($template, $data); } } ?>

Class reference:-

There are following references available in template parser class library. Now we will explain.

1. Parse.

This reference is used to parse a template from the provided path and variables.

parse($template, $data[$return = FALSE])

2. Parse string.

This reference work same like parse(). It only accept template as a string and loading a view file.

parse_string($template, $data[$return = FALSE])

3. Set delimiters.

This method is used to set opening and closing tag for pseudo variable tags in template.

set_delimiters([$l = '{'[$r = '}']])

Models

Connect Database

Helpers

Libraries

Helper Reference

Library Reference

Database Reference