symfony/twig-pack
Official Symfony pack that bundles Twig templating for Symfony apps: Twig, the Twig bridge and needed integrations. Quick install via Composer to add rendering, templates, extensions and common defaults for modern Symfony projects.
Installation Add the package via Composer in your Laravel project:
composer require symfony/twig-pack
(Note: This package is primarily a Symfony "Pack" for Twig, meaning it’s designed to integrate Twig into Symfony projects. In Laravel, you’d typically use twig/laravel or spatie/laravel-twig instead. However, if you’re working in a hybrid Laravel/Symfony environment or need Twig-specific Symfony Pack features, proceed with caution.)
First Use Case
spatie/laravel-twig).twig:install, twig:debug). Run:
php bin/console twig:install
(In Laravel, this would require manual config or a custom recipe.)Where to Look First
spatie/laravel-twig for Laravel integration.resources/views/ (if using a Laravel bridge) or templates/ (Symfony convention).Template Rendering
render function in controllers:
return $this->render('template.html.twig', ['data' => $value]);
spatie/laravel-twig, render via:
return Twig::render('template.twig', ['data' => $value]);
Dynamic Includes/Extends
{% extends 'base.twig' %}
{% block content %}
{{ include('partial/_header.twig') }}
{% endblock %}
Asset Management
twig:assets for versioned URLs:
<link rel="stylesheet" href="{{ asset('css/style.css') }}">
Form Handling
TwigBundle's form themes:
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
resources/views/ and templates/:
// config/twig.php (custom)
'paths' => [
realpath(__DIR__.'/../../resources/views'),
realpath(__DIR__.'/../../templates'),
],
# config/packages/twig.yaml
twig:
cache: '%kernel.cache_dir%/twig'
Laravel Incompatibility
spatie/laravel-twig instead for Laravel projects.Twig\Error\LoaderError if templates aren’t found in Symfony’s templates/ directory.Asset Pipeline Conflicts
asset() function may clash with Laravel’s Mix/Vite. Ensure consistent asset paths:
<!-- Symfony -->
{{ asset('css/app.css') }}
<!-- Laravel (if using Mix) -->
<link href="{{ mix('css/app.css') }}" rel="stylesheet">
Caching Quirks
var/cache/). Clear it with:
php bin/console cache:clear
php artisan view:clear for Twig views (if using spatie/laravel-twig).Dependency Overlap
symfony/http-foundation). In Laravel, this can cause conflicts with Laravel’s base classes.composer why symfony/http-foundation to audit dependencies.Template Errors: Enable Twig’s debug mode (Symfony):
twig:
debug: '%kernel.debug%'
TWIG_DEBUG=true in .env.Loader Issues: Verify template paths are correct:
php bin/console debug:container twig.loader
Custom Twig Extensions
// src/Twig/AppExtension.php
class AppExtension extends \Twig\Extension\AbstractExtension
{
public function getFunctions()
{
return [
new \Twig\TwigFunction('app_function', [$this, 'appFunction']),
];
}
}
Filters/Tests
$twig->addFilter(new \Twig\TwigFilter('custom_filter', function ($str) {
return strtoupper($str);
}));
Symfony Recipes
# resources/recipes/your-vendor/your-package.yaml
symfony/recipe:
type: project
name: Your Laravel Project
# ...
How can I help you explore Laravel packages today?