RainTPL
RainTPL is an open source PHP template engine, it load precompiled PHP templates:
- Easy, 8 Tags, 3 Methods, 1 Class in a single File
- Fast, load precompiled templates
- Useful, divide logic by design
- WYSIWYG, design HTML with img/css with relative paths, Rain will substitute with the correct paths
Installation
Downloadhow RainTPL copy, the file rain.tpl.class.php in your project include directory inc/, create a templates directory tpl/, and a cache directory tmp/.
Include and configure
include "inc/rain.tpl.class.php"; //include Rain TPL raintpl::$tpl_dir = "tpl/"; // template directory raintpl::$cache_dir = "tmp/"; // cache directory
Tags
Here the tags you need
{$Variable_name}
Variables are the dynamic contents of the templates, you can assign them with assign() method. Variables are case_sensitive.
My age is {$age} // 30
You can do simple operations such as + - * / % with numbers, string and other variables and also use = for assign a value to a variable
{$year=30} My age is {$age+$year} // 60
You can use functions with variables
My name is {$name|substr:0,3} // Rai
Use {$GLOBALS} to access all globals variable as _SESSION, _GET, _POST, _SERVER.
{#constant#}
You can constants with this syntax: {#constant#}
{if="condition"}{/if}
Is the equivalent of php if. You can use variable and php into the condition.
{if condition="$age < 20"} less than 20 years {elseif condition="$age < 30"} less than 30 years {else} 30 or more {/if}
{loop="array"}{/loop}
This tag allow you to loop an array, it's useful to print a list of articles, news.
{loop="user"} {$value.user_id} - {$value.name} - {$value.phone} {/loop}
Betweein {loop} and {/loop} you can use special variables:
- {$key}, key of the array element.
- {$value}, value of the array.
- 0, loop counter, it starts from 0. If you want starts from 1: {$counter+1}. If you do {$counter%2+1} each cycle will return 1 and 2.
{include="template"}
Use this tag for include external templates. Useful for include header and footer if they are the same in all page of your website.
{include="layout/header"}
You can also use use static cache with include tag. Check the documentation.
{function="myFunc"}
Call php functions and print the result.
{function="pagination($selected_page)"}
{* comment *}
All between {* and *} is deleted in the compiled file. Use this tag to comment your templates.
{* 'sup dude? I'm a comment *}
{noparse}{/noparse}
All the code between {noparse} {/noparse} it won't be parsed
Hey {noparse}how are you {$name}{/noparse}?
Output:
Hey how are you {$name}?
WYSIWYG - Path replace
This cool feature allows designers to create templates as regular HTML with images and styles with relative paths, RainTPL replaces automatically these paths with the correct server paths.
Absolute paths and paths ending with # will be not changed.
<link href="style.css" type="text/css" rel="stylesheet"> <img src="img/logo.gif">
Output html:
<link href="tpl/style.css" type="text/css" rel="stylesheet"> <img src="tpl/img/logo.gif">
Note: if you set raintpl::$base_url, RainTPL will replace the path with raintpl::$base_url.
Assign and draw
This is what you've to write in your script to assign variables and draw the template.
$tpl = new raintpl(); //include Rain TPL $tpl->assign( "age", "30" ); // assign variable $tpl->assign( array( "age" => "30", "name" => "Rain" ) ); // associative array $tpl->assign( "user", $user ); // assign an array $tpl->draw( "mytemplate" ); // draw the template
Is avalaible also a Cache method which can improve drastically the load speed of your page. Check the documentation.
|