php-compatible/templates

Modern PHP-style templating that works on PHP 5.5+

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/php-compatible/templates

v1.0.0 2025-12-30 21:28 UTC

This package is auto-updated.

Last update: 2025-12-30 21:44:42 UTC


README

CI codecov PHP Version License

Blazing fast PHP templating with zero dependencies.

A lightweight, high-performance template engine that leverages PHP's native output buffering for maximum speed. No parsing overhead, no compilation step, no caching layer needed - just pure PHP execution at full speed.

Why templates?

  • Zero overhead - Uses PHP's native require and output buffering. No regex parsing, no AST compilation, no runtime interpretation.
  • Instant rendering - Templates execute as native PHP code. What you write is what runs.
  • No dependencies - A single function. No framework required. No external libraries.
  • Legacy compatible - Works on PHP 5.5 through 8.x. Modernize your legacy codebase without breaking compatibility.
  • Familiar syntax - It's just PHP. No new template language to learn.

Documentation

Full documentation available at phpcompatible.dev/docs/category/templates

Installation

composer require php-compatible/templates

Quick Start

Create a template file views/hello.php:

<h1><?php echo $title; ?></h1>
<p><?php echo $message; ?></p>

Render it:

<?php
require 'vendor/autoload.php';

$html = template('views/hello.php', array(
    'title' => 'Hello World',
    'message' => 'Welcome to my site!'
));

echo $html;

Examples

Loops

<!-- views/list.php -->
<ul>
<?php foreach ($items as $item): ?>
    <li><?php echo $item; ?></li>
<?php endforeach; ?>
</ul>
$html = template('views/list.php', array(
    'items' => array('Apple', 'Banana', 'Cherry')
));

Nested Data

<!-- views/user.php -->
<div class="user-card">
    <h2><?php echo $user['name']; ?></h2>
    <p><?php echo $user['email']; ?></p>
</div>
$html = template('views/user.php', array(
    'user' => array(
        'name' => 'John Doe',
        'email' => 'john@example.com'
    )
));

HTML Escaping

The template() function does not automatically escape output. For security, use htmlspecialchars():

<h1><?php echo htmlspecialchars($title, ENT_QUOTES, 'UTF-8'); ?></h1>

API

template($path, $variables)

Renders a PHP template file and returns the output as a string.

Parameter Type Description
$path string Path to the template file
$variables array Associative array of variables available in the template

Returns: string - The rendered template output

Requirements

  • PHP 5.5 or higher

License

MIT