A simple template engine for PHP
Install the latest version with
composer require sy/template
<?php
use Sy\Template\Template;
// Create a template with variable slot
$template = new Template();
$template->setFile('mytemplate.tpl');
// Fill the variable slot
$template->setVar('NAME', 'World');
// Output render
echo $template->getRender();
The template file mytemplate.tpl content:
Hello {NAME}
The output result:
Hello World
<?php
use Sy\Template\Template;
// Create a template with a block
$template = new Template();
$template->setFile('mytemplate.tpl');
// This variable will be overrided below
$template->setVar('NAME', 'Hello world');
// Fill the variable slot and repeat the block
foreach (['foo', 'bar', 'baz'] as $name) {
$template->setVar('NAME', $name);
$template->setBlock('MY_BLOCK');
}
// Output render
echo $template->getRender();
The template file mytemplate.tpl content:
{NAME}
<!-- BEGIN MY_BLOCK -->
Hello {NAME}
<!-- END MY_BLOCK -->
The output result:
baz
Hello foo
Hello bar
Hello baz
<?php
use Sy\Template\Template;
// Create a template with a block
$template = new Template();
$template->setFile('mytemplate.tpl');
// This variable will not be overrided below because the block use isolated variables
$template->setVar('NAME', 'Hello world');
// Fill the variable slot and repeat the block
foreach (['foo', 'bar', 'baz'] as $name) {
// Use isolated variables for this block
$template->setBlock('MY_BLOCK', ['NAME' => $name]);
}
// Output render
echo $template->getRender();
The template file mytemplate.tpl content:
{NAME}
<!-- BEGIN MY_BLOCK -->
Hello {NAME}
<!-- END MY_BLOCK -->
The output result:
Hello world
Hello foo
Hello bar
Hello baz
You can set a default ouput for a block when this one is not set using the ELSE block:
<?php
use Sy\Template\Template;
// Create a template with a block
$template = new Template();
$template->setFile('mytemplate.tpl');
// No setBlock here
// Output render
echo $template->getRender();
The template file mytemplate.tpl content:
<!-- BEGIN MY_BLOCK -->
Hello {NAME}
<!-- ELSE MY_BLOCK -->
Block not set
<!-- END MY_BLOCK -->
The output result:
Block not set
You can use double or simple quotes around the slot name:
{"Hello"}, {'my name is'}...
If these slots are not set, the output will be the string inside of the simple or double quotes:
Hello, my name is...
You can set a default value for a slot when this one is not set using a slash caracter after the slot name:
Hello {NAME/John Doe}
If the slot NAME
is not set, the output will be:
Hello John Doe
The slot default behavior changed on the version 2. Before, on the version 1, an unset slot is cleared on output:
Hello {NAME}
If the slot NAME
is not set, the output will be:
Hello
Starting at version 2:
Hello {NAME}
If the slot NAME
is not set, the output will be:
Hello {NAME}
You can use the "Slot default value" feature to achieve that. The slot will be replaced by the string after the slash, so if you put an emtpy string after the slash, the slot will be replaced by an empty string:
Hello {NAME/}
If the slot NAME
is not set, the output will be:
Hello