sy/template

Simple template engine for PHP

1.1.0 2021-12-23 18:19 UTC

This package is auto-updated.

Last update: 2022-01-23 18:41:10 UTC


README

A simple template engine for PHP

Installation

Install the latest version with

$ composer require sy/template

Basic Usage

Variables

<?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

Blocks

<?php

use Sy\Template\Template;

// create a template with a block
$template = new Template();
$template->setFile('mytemplate.tpl');

// 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

<!-- BEGIN MY_BLOCK -->
Hello {NAME}
<!-- END MY_BLOCK -->

The output result

Hello foo
Hello bar
Hello baz