Skip to content

Documentation for web designers

Babar336 edited this page Feb 23, 2022 · 21 revisions

RainTPL is an easy PHP template engine, it loads simple HTML templates with tags. A template can looks as the following index.html file:

    <!DOCTYPE html>
    <html>
    <head>
    <title>{$title}</title>
    </head>
    <body>
        {$content}
    </body>
    </html>

By default the template are saved into the themes folder, which can be changed in the configuration.

Tags

  • {$variable}
  • {#constant#}
  • {include="template"}
  • {if="expression"} v
  • {loop="$array"}
  • {function="function name"}
  • {* comment *} or {ignore}
  • {noparse}
  • {autoescape="off"}

{$variable}

Variables are the dynamic contents of the template, valorized in the execution of the script with RainTPL assign() method. Variable names are case-sensitive.

example

    {$name}, I am your father!

output

    Luke, I am your father!

You can access array elements using the . operator and object properties using -> operator:

example

   {$array.key} 
   {$object->element}

modifiers and operations on variables

You can execute mathematical operations with variables, assignment and even add modifiers. Modifiers are function executed on the variables

example

   {$a=1}
   {$b=2}
   {$sum=$a+$b}

example

    {$title|cut:10} // Hello Wor...
    {$title|touppercase|cut:7} // HELLO W...

{#constant#}

You can access the constants defined in the PHP code using the tag {#constant#}

example

    {#URL#}

output

    http://raintpl.com

{include="template"}

With this tag you can include external template as blocks, for example you can include the header and the footer:

example

    <html>
    <body>
    {include="header"}
    here goes the content of the website
    {include="footer"}
    </body>

note: you can use a variable to dynamically load a template, for example {include="$page"}

{if="expression"}

Checks an expression and print the code between {if}{else} if the condition is true or {else}{/if} if the condition is false:

example

    {if="$age<18"}
         minor
    {else}
         adult
    {/if}

{loop="$var"}

This tag allow to loop through the value of arrays or objects.

example

    <ul>
    {loop="$weekday"}
        <li>{$key}, {$value}</li>
    {/loop}
    </ul>

output

    <ul>
        <li>0, Sunday</li>
        <li>1, Monday</li>
        ...
        <li>6, Saturday</li>
    </ul>

Between {loop} and {/loop} few variables take special values such as:

  • {$key}, the key of the current element of the array
  • {$value}, the value of the current element of the array
  • {$counter}, count the loops starting from 0 increasing each time by 1

Also between {loop} and {/loop} allows you use:

  • {break}, for terminate the loop
  • {continue}, for continue into the loop

{function="function name"}

Use this tag to execute a PHP function and print the result. You can pass strings, numbers and variables as parameters.

example

    {function="date('%Y')"}

output

    2013

{* comment *} or {ignore}

Everything between {* and *} is commented out:

example

    Hey {* {$name} what's up *} dude?

output

    Hey dude?

using {ignore} and {/ignore}:

example

    this is a test
    {ignore}
    the {$var} inside the ignore
    {/ignore}
    not to much important.

output

    this is a test
    not to much important.

{noparse}

Everything between {noparse} and {/noparse} won't be compiled:

example

    {noparse}this is the {$title}{/noparse}

output

    this is the {$title}

{autoescape="off"}

By default autoescape is on, but you can use to escape HTML code:

example

    {autoescape="off"}{$safe_variable}{/autoescape}

if autoscapes is off, the result is:

output

    <script>console.log('this is safe')</script>

but if you not use autoscape, and if has the default value on, the result is this:

output

    &lt;script&gt;console.log('this is safe')&lt;/script&gt;