phpdot/template

Swoole-safe Twig integration with auto-discovered extensions for the PHPdot ecosystem.

Maintainers

Package info

github.com/phpdot/template

Issues

pkg:composer/phpdot/template

Transparency log

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

v0.1.0 2026-07-17 23:14 UTC

This package is auto-updated.

Last update: 2026-07-18 03:25:26 UTC


README

Swoole-safe Twig integration for PHPdot. A single Twig\Environment is built once per worker and shared across coroutines; Twig extensions are auto-discovered from installed package manifests and resolved through the container. Application code touches only the small View API.

Table of Contents

Requirements

Requirement Constraint
PHP >= 8.5
phpdot/package ^0.1
psr/container ^2.0
twig/twig ^3.10

phpdot/config and phpdot/container are dev-only suggestions — the #[Config('template')] / binding attributes are inert until a phpdot application reflects them.

Installation

composer require phpdot/template

Usage

Three objects; your application code touches only View:

use PHPdot\Template\EngineFactory;
use PHPdot\Template\TemplateConfig;
use PHPdot\Template\View;

$config  = new TemplateConfig(paths: ['__main__' => [__DIR__ . '/views']]);
$factory = new EngineFactory($config, $manifest, $container);
$view = new View($factory);

echo $view->render('hello.twig', ['name' => 'Omar']);

The View API

$view->render('mail/welcome.twig', ['user' => $user]);
$view->renderBlock('mail/welcome.twig', 'subject', ['user' => $user]);  // one block
$view->exists('admin/dashboard.twig');                                  // bool

$twig = $view->environment();   // escape hatch to the underlying Twig\Environment

TemplateConfig carries namespaced paths, an optional compiled-template cache, and the debug, strictVariables, autoReload, and autoescape flags.

Architecture

View delegates to EngineFactory, which builds the shared Twig\Environment on first use — wiring the filesystem loader from the configured paths and registering every Twig extension advertised by an installed package's Manifest, each resolved as a singleton through the container. The environment is built once and reused, which keeps it safe to share across Swoole coroutines.

graph TD
    APP["Application"]
    VIEW["View<br/><br/>render / renderBlock / exists"]
    FACTORY["EngineFactory<br/><br/>builds + caches the Twig Environment"]
    CONFIG["TemplateConfig<br/><br/>paths, cache, debug flags (#[Config])"]
    MANIFEST["Package Manifest + container<br/><br/>auto-discovered Twig extensions (singletons)"]
    TWIG["Twig\\Environment<br/><br/>one per worker, shared across coroutines"]

    APP --> VIEW
    VIEW --> FACTORY
    CONFIG --> FACTORY
    MANIFEST --> FACTORY
    FACTORY --> TWIG
Loading

Testing

composer install
composer test        # PHPUnit
composer analyse     # PHPStan, level max + strict rules
composer cs-check    # PHP-CS-Fixer
composer check       # All three

License

MIT.

This repository is a read-only mirror, generated by CI from phpdot/monorepo. Pull requests and issues belong in the monorepo.