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).
Put filename without extension as first parameter.
$this->parser->parse('template_file');
$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.
<h1>{pseudo-variable}</h1>
<p>{content}</p>
<small>Author: {author}</small>
Multi-dimensional array:
$data = array
(
'pseudo-variable' => 'Some data',
'content' => 'Some new exciting content',
'author' => array( 'name' => 'Tomas', 'email' => '[email protected]' )
);
Can be displayed using arrow ( -> ):
<h1>{pseudo-variable}</h1>
<p>{content}</p>
<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.
{lang->{current_lang}->error_msg}
$data = array('user' => array
(
array('username' => 'life', 'email' => '[email protected]', 'address' => 'Earth 001, Milky way'),
array('username' => 'anonym', 'email' => '[email protected]', '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>
</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: {user.name}
<!-- BEGIN {user.friends} AS friend -->
Name: {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>
</div>
<!-- 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 -->
Monday
<!-- ELSEIF {day} == 2 -->
Thuesday
<!-- 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 |
Your folder structure might look like (if you want to use themes):
/views/
/my_theme
/css
style.css
/img
/js
/tpl (this folder is required)
template_file.tpl
/my_second_theme
/css
style.css
/img
/js
/tpl (this folder is required)
template_file.tpl
You can simply switch between themes:
$this->parser->theme( 'my_second_theme' );
Passing data into parser outside of parser() function. Can be used in models that are autoloaded.
$this->parser->append( 'urls', array( 'apple' => array( 'url' => 'http://apple.com', 'title' => 'Apple Homepage') ) );
$this->parser->parse( 'template' );
<a href="{urls->apple->url}"> {urls->apple->title} </a>
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 syntax:
$parser = new parser;
$parser->append();
$parser->theme();
$parser->parse();
CI syntax:
$this->load->library( 'ciparser' );
$this->parser->append();
$this->parser->theme();
$this->parser->parse();
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.