1. Go to this page and download the library: Download phppkg/easytpl 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/ */
phppkg / easytpl example snippets
use PhpPkg\EasyTpl\EasyTemplate;
$tplCode = <<<'CODE'
My name is {{ $name | strtoupper }},
My develop tags:
{{ foreach($tags as $tag) }}
- {{ $tag }}
{{ endforeach }}
CODE;
$t = new EasyTemplate();
$str = $t->renderString($tplCode, [
'name' => 'inhere',
'tags' => ['php', 'go', 'java'],
]);
echo $str;
use PhpPkg\EasyTpl\EasyTemplate;
$t = EasyTemplate::new([
'tplDir' => 'path/to/templates',
'allowExt' => ['.php', '.tpl'],
]);
// do something ...
First value is: {{ $arr.0 }} // val0
'subKey' value is: {{ $arr.subKey }} // val1
{{ if ($name !== '') }}
hi, my name is {{ $name }}
{{ endif }}
hi, my name is {{ $name }}
age is {{ $age }}, and
{{ if ($age >= 20) }}
age >= 20.
{{ else }}
age < 20.
{{ endif }}
hi, my name is {{ $name }}
age is {{ $age }}, and
{{ if ($age >= 50) }}
age >= 50.
{{ elseif ($age >= 20) }}
age >= 20.
{{ else }}
age < 20.
{{ endif }}
use PhpPkg\EasyTpl\EasyTemplate;
$tpl = EasyTemplate::new();
// use php built function
$tpl->addFilter('upper', 'strtoupper');
// add more
$tpl->addFilters([
'last3chars' => function (string $str): string {
return substr($str, -3);
},
]);