Search by

pipe / view

unamatasanatarai

Blazingly fast php templating engine

v1.2.0 2026-12-02 17:22 UTC

This package is auto-updated.

Last update: 2026-12-02 17:24:29 UTC


README

Simple, pure PHP template rendering

Latest Stable Version Total Downloads License

Installation

composer require pipe/view

Requires PHP 8.2 or newer. No runtime dependencies.

Usage

PHP is the template language. Pipe\view() returns a render function for one views directory:

use function Pipe\view;

$render = view(__DIR__ . '/views');

echo $render('layouts/main.php', [
    'title' => 'About',
    'content' => $render('about-content.php', ['message' => 'Welcome']),
]);

views/layouts/main.php:

<!DOCTYPE html>
<title><?= htmlspecialchars($title) ?></title>
<main><?= $content ?></main>
<footer><?= $render('partials/footer.php') ?></footer>

API

Function Description
Pipe\view(string $views_path = './'): callable Returns fn(string $path, array $vars = []): string.
$render(string $path, array $vars = []): string Renders $views_path/$path and returns its output. Throws Pipe\ViewNotFoundException, a RuntimeException, when the template or the views directory doesn't exist.
Pipe\views(string $views_path): void Sets the views directory for Pipe\render(). Throws Pipe\ViewNotFoundException at once when the directory doesn't exist. Calling it again switches the directory.
Pipe\render(string $path, array $vars = []): string $render for the directory set with Pipe\views(), callable anywhere. Throws LogicException before Pipe\views() was called.
Pipe\e(string|int|float|bool|null|Stringable $value): string Escapes a value for HTML text and attribute values (htmlspecialchars with ENT_QUOTES | ENT_SUBSTITUTE, UTF-8).

Inside a template, each key of $vars is a local variable, and $render is available for partials and layouts. Templates see only those variables.

One views directory for the whole app

When every page renders from the same directory, set it once in your setup code and call Pipe\render() anywhere:

Pipe\views(__DIR__ . '/templates');           // setup, once

// in a page, a command or a template
<?= Pipe\render('header.php', ['title' => 'About']) ?>
<h1>Hello, <?= Pipe\e($name) ?></h1>
<?= Pipe\render('footer.php') ?>

Rules

  • Path safety. Template paths are resolved with realpath() and must stay inside the views directory. Paths that use .. to leave it, paths that contain a NUL byte, symlinks that point outside it, and directories are all rejected as "not found". A leading / is treated as relative to the views directory. A .. that stays inside the directory still works. Even so, never build template paths from user input.
  • No escaping. Output is not escaped automatically. Escape every dynamic value yourself with Pipe\e($value).
  • Output buffers. If a template throws, the exception propagates and every output buffer the render opened is closed. A template that leaves its own buffer open still has its output collected.

Testing

composer install
composer test

Philosophy

  1. Small is Beautiful.
    • Do one thing,
    • Do one thing well.
  2. Get the PoC out ASAP.
    • Consider your code dead two weeks after inception.
  3. n'joy!

sounds familiar? It should!