PHP code example of wackowiki / templatest

1. Go to this page and download the library: Download wackowiki/templatest library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wackowiki / templatest example snippets




use Templatest\Templatest;

$tpl = Templatest::read('hello.tpl', '/path/to/cache');

$tpl->title         = 'My Page';
$tpl->name          = 'World';
$tpl->items_item_text = 'first';   // underscore-split path: items → item → text
$tpl->items_item_text = 'second';  // second iteration of same pattern

echo $tpl;

function my_pull(bool $is_block, string $loc, ...$args): string

$tpl = Templatest::read('page.tpl', __DIR__ . '/cache');

$tpl->name = 'Alice';              // set top-level variable
$tpl->user_name = 'Alice';         // underscore-split: 'user' → 'name'

$count = (int) $tpl->name;         // number of times 'name' was set

$tpl->enter('user_');              // push context — variable names get 'user_' prefix
$tpl->name = 'Alice';              // → 'user_name'
$tpl->age  = 30;                   // → 'user_age'
$tpl->leave();                     // pop context

$tpl->pull('csrf', function (bool $is_block, string $loc) {
    return '<input type="hidden" name="_token" value="' . bin2hex(random_bytes(16)) . '">';
});

$header = $tpl->header;           // clones the template rooted at pattern 'header'
$header->title = 'Welcome';
echo $header;                     // renders only the header pattern

$tpl('name', 'Alice', 'age', 30);  // equivalent to $tpl->set('name', 'Alice', 'age', 30)

$tpl->set('name', 'Alice');                        // underscore-split path
$tpl->set(['name' => 'Alice', 'age' => 30]);       // array form
$tpl->set('user', 'name', 'Alice');                // explicit nested path

$escaper = new TemplatestEscaper();
echo $escaper->escape_html('<b>x</b>');    // &lt;b&gt;x&lt;/b&gt;
echo $escaper->escape_js("'");             // \x27
echo $escaper->escape_url('hello world');  // hello%20world

src/
├── Templatest.php         # compiler entry point; reads & caches templates
├── TemplatestUser.php     # public API; per-instance renderable
├── TemplatestSetter.php   # assigns values into chunks; runs filter pipes
├── TemplatestFilters.php  # built-in filter registry
├── TemplatestEscaper.php  # context-aware string escaping (Zend-derived)
├── Helper.php             # standalone utilities (path, serialize, stringify, etc.)
└── Exception/
    ├── InvalidArgumentException.php
    └── RuntimeException.php

$tpl = Templatest::read('page.tpl');
$tpl->title = 'Hello';
echo $tpl;    // __toString() → commit(root)
                  ├── iterate pull actions
                  ├── recurse into subpatterns
                  └── implode chunks