splinter / view
Twig view rendering extension for the Splinter framework
Requires
- php: ^8.2
- splinter/framework: ^0.2
- twig/twig: ^3.8
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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
TwigServiceProviderregisteringTwig\Environmentand its filesystem loader in the DI container. - A
TwigExtensionInterfacefor adding custom Twig globals, filters, and functions without touching this package. - An
HtmlExceptionHandler, which replaces the core's defaultJsonExceptionHandlerand 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.php → extensions. 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.php → routes 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