Search by

splinter / view

Twig view rendering extension for the Splinter framework

Maintainers

Package info

github.com/vhar/splinter-view

pkg:composer/splinter/view

Transparency log

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

0.1.0 2026-08-26 15:53 UTC

This package is auto-updated.

Last update: 2026-08-26 15:56:14 UTC


README

Twig view rendering extension for the Splinter framework.

This package adds optional Twig-based templating on top of Splinter's core, without changing the core itself. It provides:

  • A TwigServiceProvider registering Twig\Environment and its filesystem loader in the DI container.
  • A TwigExtensionInterface for adding custom Twig globals, filters, and functions without touching this package.
  • An HtmlExceptionHandler, which replaces the core's default JsonExceptionHandler and renders uncaught exceptions as HTML using Twig templates.
  • Stub files (config, error templates, a demo route and controller) published into your application skeleton via vendor:publish.

Installation

composer require splinter/view

Add the provider to config/app.php:

'providers' => [
    // ...
    \Splinter\View\TwigServiceProvider::class,
],

Publish the package's config, error templates, demo route, and demo view into your application:

php splinter vendor:publish

This copies:

Source Destination
config/twig.php config/twig.php
resources/views/errors/ resources/views/errors/
resources/views/hello.twig resources/views/hello.twig
routes/web.php routes/web.php

Existing files are skipped by default. Pass --force to overwrite them.

Configuration

config/twig.php:

return [
    'views_path' => __DIR__ . '/../resources/views',
    'cache' => false,
    'auto_reload' => true,
    'extensions' => [
        // \App\View\LegacyHelpersExtension::class,
    ],
];
Key Description
views_path Directory Twig loads templates from.
cache false, or a path to a directory for compiled template caching.
auto_reload Whether Twig recompiles templates when the source file changes.
extensions List of TwigExtensionInterface classes to apply to the environment.

Adding custom Twig functions/filters

Implement TwigExtensionInterface:

namespace App\View;

use Splinter\View\TwigExtensionInterface;
use Twig\Environment as TwigEnvironment;
use Twig\TwigFilter;

final class LegacyHelpersExtension implements TwigExtensionInterface
{
    public function extend(TwigEnvironment $twig): void
    {
        $twig->addFilter(new TwigFilter('human_time', function ($seconds) {
            // ...
        }));
    }
}

Register it in config/twig.phpextensions. It is resolved from the DI container, so constructor dependencies are autowired as usual.

HTML error pages

Once the provider is registered, ExceptionHandlerInterface resolves to HtmlExceptionHandler automatically — the core's HttpKernel picks it up without any further wiring.

HtmlExceptionHandler renders errors/{statusCode}.twig for the resolved HTTP status code, falling back to errors/500.twig if a specific template isn't found, and finally to a plain inline HTML string if Twig itself fails to render.

Edit the published templates in resources/views/errors/ to match your application's design.

Demo route

routes/web.php (published, not autoloaded — add it to config/app.phproutes yourself):

'routes' => [
    'routes/api.php',
    'routes/web.php',
],

It registers GET /hello, resolved to HelloController, rendering resources/views/hello.twig. Use it to confirm the view stack renders end-to-end, then remove it once you have real routes.

Requirements

  • PHP ^8.2
  • splinter/framework
  • twig/twig ^3.8