Giter Site home page Giter Site logo

comper-template-parser's Introduction

COMPER Template Parser v 3.0.1

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).

Docs

parse( $source, $data, $config )

$source

Parsing files

Put filename without extension as first parameter.

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

$data

Pseudo-variables

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}
Cycles
$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 -->
Conditions

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 -->

$config

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):

/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' );

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' => 'http://apple.com', 'title' => 'Apple Homepage') ) );
$this->parser->parse( 'template' );
<a href="{urls->apple->url}"> {urls->apple->title} </a>

Template only syntax

Includes

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 -->

Nocode

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;

$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

Future

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.

comper-template-parser's People

Contributors

tomasbonco avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.