awsm/lib-template

Awesome template functionality. Easy and clean text template handling.

dev-develop 2020-09-22 11:20 UTC

This package is auto-updated.

Last update: 2024-12-22 20:08:46 UTC


README

Template helper classes.

Installation

Run the installation with composer.

composer require awsmug/lib-template:dev-develop

Howto

Text Template

Use of TextTemplate class:

$template = 'Hello {name}!';

$values = ['name' => 'John'] ;

$content = TextTemplate::init( $template, $values )->render();

Template file

Content in template.txt:

Hello {name}!

Use of TemplateFile class:

$template_file = 'template.txt';

$values = ['name' => 'John'] ;

$content =  TemplateFile::init( $template_file, $variables )->render();

PHP Template file

Simple variant

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();

Variant with PHP variables

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();

Setting delemiters

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();