Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Twig Laravel Package

twig/twig

Twig is a fast, secure, and flexible templating engine for PHP. It offers familiar syntax, automatic escaping, template inheritance, filters/functions, and great performance with compiled templates—ideal for building clean, maintainable views in web apps.

View on GitHub
Deep Wiki
Context7

Twig, the flexible, fast, and secure template language for PHP

Frequently asked questions about Twig
Can I use Twig alongside Blade in Laravel without conflicts?
Yes, Twig integrates seamlessly with Laravel’s Blade. The `twig/twig-bridge` package handles view resolution automatically, so you can use Twig for specific templates (e.g., CMS sections) while keeping Blade for the rest. No configuration changes are needed—just register the Twig service provider in `config/app.php`.
What Laravel versions support Twig v3.28.0?
Twig v3.28.0 works with Laravel 8.x, 9.x, and 10.x. For Laravel 7.x, use Twig v2.x. The package maintains backward compatibility, but PHP 8.5+ is recommended for full feature support (e.g., enum handling). Check your `composer.json` constraints to avoid version mismatches.
How do I migrate from Blade to Twig in an existing Laravel app?
Start by installing `twig/twig` and `twig/twig-bridge`, then update your `config/view.php` to include Twig’s resolver. Replace Blade directives (e.g., `@foreach`) with Twig syntax (`{% for %}`). Use `php artisan twig:install` to generate a basic Twig config. Test incrementally—critical paths first—to catch syntax or logic differences early.
Will Twig’s new Markup objects break my existing templates?
Yes, functions like `include()` now return `Markup` objects instead of raw strings, which may break templates using `{{ include('partial') }}` directly. Fix this by adding `|raw` (e.g., `{{ include('partial')|raw }}`). Run `twig-lint` in CI to catch affected templates before deployment. This change improves security by enforcing HTML escaping.
How do I enable sandboxing for user-uploaded Twig templates (e.g., CMS themes)?
Configure sandbox policies in `config/twig.php` by setting `sandbox` to `true` and defining allowed tags/filters. Use the `always_allowed_in_sandbox` flag for trusted functions (e.g., `{{ my_safe_filter|always_allowed_in_sandbox }}`). Avoid allowing dangerous functions like `include` or `set` in user-controlled templates to prevent code execution risks.
Does Twig support Laravel’s caching system (Redis, file) for compiled templates?
Absolutely. Twig leverages Laravel’s caching layer out of the box. Enable caching in `config/twig.php` with `'cache' => true` and specify a path (e.g., `storage/framework/views`). For Redis, ensure your `FileCache` or `RedisCache` is configured in Laravel’s `config/cache.php`. Cached templates reduce runtime overhead significantly.
How do dynamic macros in Twig v3.28.0 work, and do they replace Blade components?
Dynamic macros (e.g., `{{ macro_name.argument }}`) let you call macros with arguments, like `{{ alert('warning', 'Message') }}`. They’re more flexible than Blade components but require updating custom Twig extensions. For Laravel, use them for reusable UI elements (e.g., modals, alerts) while keeping Blade for complex logic-heavy components.
What’s the performance impact of Twig’s CorrectnessNodeVisitor in development?
The `CorrectnessNodeVisitor` adds minimal overhead in development—primarily during template parsing—to catch syntax errors (e.g., unclosed blocks). In production, compiled templates bypass this check entirely, so runtime performance remains unchanged. Profile with `twig:profile` in Laravel’s debug mode if you suspect bottlenecks in large templates.
Are there alternatives to Twig for Laravel that offer similar flexibility?
Blade is Laravel’s native templating engine and integrates tightly with Eloquent and other core features. For PHP-based alternatives, consider **Plates** (lightweight) or **Latte** (from Nette Framework). However, Twig stands out for its strict security model, sandboxing, and extensive plugin ecosystem (e.g., forms, i18n). Blade may suffice for simple apps, but Twig excels in complex, multi-author environments.
How do I test Twig templates in Laravel’s PHPUnit test suite?
Use Laravel’s `RefreshDatabase` trait or `TwigTestCase` (from `twig/twig-bridge`) to mock the Twig environment. Test templates by rendering them with sample data: `$html = $twig->render('template.twig', ['data' => $input]);` and assert against expected output. For sandboxed templates, enable the sandbox in tests and verify allow-lists. PHPUnit 11 is fully supported via the latest bridge package.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony