Search by

azurre / php-template

a.milenin

Simple native PHP template engine

Package info

github.com/azurre/php-template

pkg:composer/azurre/php-template

Statistics

Installs: 24

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-11-19 17:48 UTC

This package is auto-updated.

Last update: 2026-11-21 06:37:36 UTC


README

Simple native PHP template engine with zero runtime dependencies.

Templates are plain .phtml files — just PHP — where $this refers to the engine instance. No compilation, no custom syntax, no overhead.

Requirements

PHP >= 8.0

Installation

composer require azurre/php-template

Basic Usage

$engine = new \Azurre\Template\Engine(__DIR__ . '/templates');

echo $engine->render('home.phtml', [
    'title' => 'Welcome',
    'username' => 'John',
]);

templates/home.phtml:

<?php $this->layout('layout.phtml') ?>

<?php $this->section('content') ?>
<h1><?= htmlspecialchars($title) ?></h1>
<p>Hello, <?= htmlspecialchars($username) ?></p>

templates/layout.phtml:

<!DOCTYPE html>
<html lang="en">
<head><title>My App</title></head>
<body>
    <?= $this->getSection('content') ?>
</body>
</html>

Features

Rendering Templates

$engine->render('template.phtml', ['key' => 'value']);  // returns string
$engine->fetch('template.phtml');                        // alias without data

Sections

Content between section() calls is captured and retrievable in layouts:

// In template:
<?php $this->section('header') ?>
<h1>Header</h1>

<?php $this->section('body') ?>
<p>Body content</p>
// In layout:
<?= $this->getSection('header') ?>
<?= $this->getSection('body') ?>

Sections can have fallback templates:

<?= $this->getSection('sidebar', 'default-sidebar.phtml') ?>

Check if a section exists:

<?php if ($this->hasSection('sidebar')): ?>
    <aside><?= $this->getSection('sidebar') ?></aside>
<?php endif ?>

Layouts

Templates declare which layout wraps them:

<?php $this->layout('layout.phtml', ['title' => 'Page Title']) ?>

Layout data takes precedence over render data when keys conflict.

Includes (Partials)

<?php $this->include('partials/button.phtml', ['label' => 'Click me']) ?>

Persistent Data

Data set via assign() persists across multiple render() calls:

$engine->assign('siteName', 'My App');
$engine->render('page1.phtml');  // $siteName available
$engine->render('page2.phtml');  // $siteName still available

Data passed directly to render() is scoped to that single call and does not leak.

Configuration

$engine->setBaseTemplatePath('/path/to/templates');

API Reference

__construct(?string $baseTemplatesPath = null)

Create engine instance. Optionally set the base templates directory.

render(string $template, array $data = []): string

Render a template with data. Returns the rendered HTML. Throws Azurre\Template\Exception if template not found.

fetch(string $template): string

Alias for render() without additional data.

layout(string $template, array $data = []): static

Register a layout template for the next render. Called from within templates.

section(string $section): void

Begin capturing output into a named section. Called from within templates.

getSection(string $section, ?string $defaultTemplate = null): string

Retrieve captured section content. Optionally provide a fallback template if the section was never defined.

hasSection(string $section): bool

Check whether a section was captured.

include(string $template, array $data = []): void

Include a sub-template at the current position. Called from within templates.

assign(string $key, mixed $value): static

Store a persistent template variable that survives across renders.

getData(?string $key = null, mixed $defaultValue = null): mixed

Retrieve all template data, or a single key with optional default.

setBaseTemplatePath(string $baseTemplatesPath): static

Set the base directory for template lookups.

Testing

composer install
./vendor/bin/phpunit

License

MIT