PytoTPL template engine
Installation
Install via composer, (*1)
composer require pytocryto/pytotpl
Initiate & configure PytoTPL, (*2)
$tpl = new \PytoTPL\PytoTPL();
$tpl->setConfig([
'tpl_folder' => __DIR__ . '/dir/to/templates/',
'cache_folder' => __DIR__ . '/dir/to/cache/',
'tpl_file_format' => '.tpl',
'compress' => false, // compression only available in a valid <HTML> document
'event_emitter' => true, // set to false for better performance
]);
Outputs:
{$variable}
{$array.key1.key2.key3}
{echo $variable}
{print $variable}
{$variable|substr:0,-1}
Unescaped output:
{!! $variable !!}
Loops:
{foreach: $myArray as $item}
..
{/foreach}
{for: $i = 0; $i < 99; $i++}
..
{/for}
{while: $i < 9}
..
{/while}
If Statements:
{if: date('Y') == 2017}
Yey, its 2017!
{elseif: date('Y') == 2018}
Yey, its 2018!
{else}
NOOOOo :(
{/if}
PytoTPL provides a few shortcuts, instead of having to write conditions like:, (*3)
{if: isset($data)}
Yes
{else}
Nope
{/if}
{-- or --}
{if: empty($data)}
...
{/if}
you'd instead write:, (*4)
{isset: $data}
Yes
{else}
Nope
{/isset}
{-- or --}
{empty: $data}
...
{/empty}
Conditions
{continue}
{break}
````
Instead of writing the long-way condition, e.g:
```html
{if: $userid == 1}
{continue}
{/if}
You can simply include the condition in one line:, (*5)
{continue: $userid == 1}
{break: $userid == 1}
Extending/including an layout:
{extend 'header'}
Set a variable:
Note: When setting a variable with PytoTPL-syntax you don't have to end it with a semicolon ";", (*6)
{var $myVariable = 'My value here'}
{-- or --}
{assign $myVariable = 'My value here'}
Using ternary operators:
{$_GET['id'] OR 'No id has been set!'}
{-- Anything here is just a PHP comment --}
Sections (NOT TESTED YET!):
Note: Section names must be written as word characters., (*7)
{section: mySection}
...
{/section}
If you want to pass variables to the section you may do so:, (*8)
{section: mySection, varName=Value}
...
{/section}
You can also overwrite a section using the following syntax, (*9)
{section.overwrite: mySection}
...
{/section}
To render a section
{section.view: mySection}