pytocryto/pytotpl

A PHP template engine

1.0.0 2018-04-26 19:37 UTC

This package is not auto-updated.

Last update: 2024-03-22 17:19:18 UTC


README

Installation

Install via composer

composer require pytocryto/pytotpl

Initiate & configure PytoTPL

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

{if: isset($data)}
    Yes
{else}
    Nope
{/if}

{-- or --}

{if: empty($data)}
    ...
{/if}

you'd instead write:

{isset: $data}
    Yes
{else}
    Nope
{/isset}

{-- or --}

{empty: $data}
    ...
{/empty}

Conditions

{continue}
{break}

Instead of writing the long-way condition, e.g:

{if: $userid == 1}
    {continue}
{/if}

You can simply include the condition in one line:

{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 ";"

{var $myVariable = 'My value here'}

{-- or --}

{assign $myVariable = 'My value here'}

Using ternary operators:

{$_GET['id'] OR 'No id has been set!'}

Comments:

{-- Anything here is just a PHP comment --}

Sections (NOT TESTED YET!):

Note: Section names must be written as word characters.

{section: mySection}
...
{/section}

If you want to pass variables to the section you may do so:

{section: mySection, varName=Value}
...
{/section}

You can also overwrite a section using the following syntax

{section.overwrite: mySection}
...
{/section}
To render a section
{section.view: mySection}