Template helper classes.
Run the installation with composer.
composer require awsmug/lib-template:dev-develop
Use of TextTemplate class:
$template = 'Hello {name}!';
$values = ['name' => 'John'] ;
$content = TextTemplate::init( $template, $values )->render();
Content in template.txt:
Hello {name}!
Use of TemplateFile class:
$template_file = 'template.txt';
$values = ['name' => 'John'] ;
$content = TemplateFile::init( $template_file, $variables )->render();
Content in Template.php:
<?php
echo 'Hello {name}!'
Use of PhpTemplateFile class:
$template_file = 'template.php';
$values = ['name' => 'John'] ;
$content = PhpTemplateFile::init( $template_file, $values )->render();
Content in Template.php:
<?php for( $i = 0; $i < $variables['count']; $i++ ): ?>
{content}
<?php endforeach; ?>
Use of PhpTemplateFile class:
$template_file = 'template.php';
$values = ['content' => 'This is my content'] ;
$variables = [ 'count' => 5 ];
$content = PhpTemplateFile::init( $template_file, $values, $variables )->render();
By default delimiters for variables are '{' and '}'. You can change this with the method method setDelimiters( $openDelemiter, $closeDelimiter ) in all Template classes.
$template = 'Hello [name]!';
$values = ['name' => 'John'] ;
$content = TextTemplate::init( $template, $values )->setDelimiters('[',']')->render();