Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

COMPER Template Parser

branch: master

Fixed notes

latest commit fa577d99f5
tomasbonco authored February 13, 2014
Octocat-spinner-32 LICENSE Initial commit November 28, 2013
Octocat-spinner-32 Parser.php Fixed notes February 13, 2014
Octocat-spinner-32 Parser_PHP.php Fixed notes February 13, 2014
Octocat-spinner-32 Update February 13, 2014

COMPER Template Parser v 3.0

COMPER Template Parser is template engine written in PHP, which separates logic (PHP code) from design (templates - HTML). We believe that you should have pure PHP without any HTML code, and pure HTML without any PHP code. It helps you to be productive and makes your code much more readable, modular and clean for long time. Since 2010 there is version for CodeIgniter (PHP Framework).


parse( $source, $data, $config )


Parsing files

Put filename without extension as first parameter.

Parsing strings
$this->parser->parse('Hello!', array(), array('is_string' => TRUE));



Defined as simple array.

$data = array
    'pseudo-variable' => 'Some data',
    'content' => 'Some new exciting content',
    'author' => 'That is me!'

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

Shown using {} brackets.

<small>Author: {author}</small>

Multi-dimensional array:

$data = array
    'pseudo-variable' => 'Some data',
    'content' => 'Some new exciting content',
    'author' => array( 'name' => 'Tomas', 'email' => '' )

Can be displayed using arrow ( -> ):

<small>Author: {author->name} ({author->email})</small>

You can go to unlimited deep.

Modificators can be used as much as you need, with parameters:

{name|trim|ucfirst|str_replace[&, " ", "-"]}

Ampersand (&) stands for 'this'. Variables inside variables can also be used.

$data = array('user' => array
    array('username' => 'life', 'email' => 'life@earth.zz', 'address' => 'Earth 001, Milky way'),
    array('username' => 'anonym', 'email' => 'seeyou@friday.zz', 'address' => 'Paris, France')

In TPL's defined using syntax:

<!-- BEGIN {user} -->
<div class="user">
    <div> Username: {username} </div>
    <div> Email: {email} </div>
    <div> Address: {address} </div>
<!-- END {user} -->

Naming cycle as recommended. You can also use prefixes and nested cycles. Notice BEGIN and END formula for naming cycle and second line for prefix.

<!-- BEGIN {user} AS user -->

    Name: {}

    <!-- BEGIN {user.friends} AS friend -->
        Name: {}
    <!-- END friends -->

<!-- END user -->

There is special pseudo-variable generated by cycles {__i} as iterator of current loop.

<!-- BEGIN {user} AS user -->

    <!-- IF {__i} == 0 -->

        <div class="main">

            <img src="{image}">
            <p> {content }</p>


    <!-- ELSE -->

        <div class="featured"> {content} </div>

    <!-- END -->

<!-- END user -->

Are very intuitive. You can use php functions and also modificators (recommended).

<!-- IF {users|count} > 0 -->

    <!-- BEGIN users -->
    <!-- END users -->

<!-- ELSE -->
<!-- END -->
<!-- IF {day} == 1 -->
<!-- ELSEIF {day} == 2 -->
<!-- ELSEIF {day} == 3 -->
<!-- END -->
<!-- IF is_numeric( {pow} ) && pow( {num}, 2 ) > 4 -->
<!-- ELSE -->
<!-- END -->


Note: There are differences between PHP and CodeIgniter version.

option default value meaning
allow_modificators TRUE Allow modificators
append array() Set 'common' arrays for append() function
clean TRUE Delete pseudovalues
disable_appends FALSE Turns appends off
disable_conditions FALSE Turns conditions off
disable_cycles FALSE Turns cycles off
disable_includes FALSE Turns includes off
disable_variables FALSE Turns variables off
exceptions array() Ignored pseudo-variables
extension 'tpl' Extension of templates
is_string FALSE Parse from string (when false, it's parsing files)
path '.' Location of templates
show TRUE Show the result
suffix_theme_only TRUE Use template_suffix only when themes are used
template_suffix 'tpl' Directory of templates in view folder
theme '' Set theme for your project

theme( $theme_name )

Your folder structure might look like (if you want to use themes):

        /tpl (this folder is required) 

        /tpl (this folder is required) 

You can simply switch between themes:

$this->parser->theme( 'my_second_theme' );

append( $name, $array )

Passing data into parser outside of parser() function. Can be used in models that are autoloaded.

$this->parser->append( 'urls', array( 'apple' => array( 'url' => '', 'title' => 'Apple Homepage') ) );
$this->parser->parse( 'template' );
<a href="{urls->apple->url}"> {urls->apple->title} </a>

Template only syntax


You can load one template into another one. Data are global, so can be used in both templates. You are writting filename without extension.

<!-- INCLUDE header -->


To specify area of template that will NOT be parsed use (very useful when using Angular.js):

    Value: {item}
    Syntax: <!-- NOCODE --> {item} <!-- /NOCODE -->

PHP vs. CodeIgniter version

PHP syntax:

$parser = new parser;


CI syntax:

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


CI configuration differences:

option default value meaning
append array( 'config' => CI_Config ) Set 'common' arrays for append() function
exceptions array('memory_usage', 'elapsed_time') Ignored pseudo-variables
path '%path%/views/' Location of templates


As I switched to Node.JS, I'm not planning to support this project any longer. That's why I finished version 3, closed CTP website and moved to github. I want to give this to the comumnity as a gift and I want community to take care about it. I'll try to answer Issues and also Pull new requests (but not very often). You guys are free to continue in my work any way you like, fork it, change it, improve it, build your own project upon it or pull a request. I would be really very glad if you mension me if you get ispired or use some parts of this work in your projects, but I don't require it.

Something went wrong with that request. Please try again.