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 Hooks Laravel Package

sylius/twig-hooks

Sylius Twig Hooks is a lightweight component from the Sylius Stack initiative that adds a hook-based extension system for Twig templates, making it easier to customize and extend views in a structured, decoupled way.

View on GitHub
Deep Wiki
Context7

[READ-ONLY] Composable Twig layouts

Frequently asked questions about Twig Hooks
How do I integrate Sylius Twig Hooks into an existing Laravel project using Twig?
First, install via Composer: `composer require sylius/twig-hooks`. Then register the Twig extension manually in your `AppServiceProvider` by binding it to the Twig environment. Ensure your Laravel version supports Twig 2.x (Laravel 9+ by default). No native Laravel service provider is included, so explicit bootstrapping is required.
Can I use Twig Hooks for dynamic layouts in a Laravel + Inertia.js app?
Yes, Twig Hooks works well with Inertia.js for server-side rendering (SSR). You can inject dynamic content like headers or footers via hooks in your Twig templates, which Inertia.js will then render on the client side. This is especially useful for modular layouts where content changes based on user roles or feature flags.
What Laravel versions are officially supported by Sylius Twig Hooks?
The package is tested with Laravel 9+ (which uses Twig 2.x by default). For Laravel 8, you’ll need to manually specify Twig 2.12.x via Composer. Always check the package’s `composer.json` for the latest compatibility notes, as minor updates may introduce breaking changes.
How do I debug issues when a Twig hook isn’t firing as expected?
Use Twig’s built-in `{% debug %}` tag to inspect the template context and verify hook registration. Check your service container or middleware for proper hook binding. If hooks are registered dynamically (e.g., via middleware), ensure the logic executes before the template is rendered. Logging hook calls in your Twig extension can also help trace the issue.
Are there performance concerns with using many Twig hooks in production?
Hooks introduce minimal runtime overhead due to reflection for resolution, but excessive hooks (e.g., hundreds per request) could impact Twig compilation. For production, consider caching hook resolutions or limiting hooks to critical sections. Profile your templates to identify bottlenecks, especially if using dynamic hook registration.
Can I use Sylius Twig Hooks with Laravel Blade templates instead of Twig?
No, this package is designed specifically for Twig and won’t work with Blade templates. If you’re using Blade, consider alternatives like `@stack`/`@push` directives or custom Blade components for modular layouts. However, Twig Hooks offers more flexibility for complex, dynamic layouts if you’re already using Twig.
How do I handle stateful data in Twig hooks (e.g., passing variables between hooks)?
Hooks are stateless by design, so you’ll need to manage state externally. Use Laravel’s service container, session storage, or dependency injection to pass data between hooks. For example, store variables in the session before rendering and access them in subsequent hooks via `app('session')->get()`. Avoid complex state logic within hooks themselves.
What’s the difference between Twig Hooks and Laravel’s Blade `@stack`/`@push` directives?
Twig Hooks provides a more flexible, decoupled system for dynamic template injection, especially useful for modular architectures like micro-frontends or headless CMS. Blade’s `@stack`/`@push` is simpler and built into Laravel but lacks the runtime customization and isolation of hooks. Choose Blade for basic modularity and Twig Hooks for advanced, dynamic layouts.
Can I use Sylius Twig Hooks for A/B testing or feature flags in Laravel?
Yes, Twig Hooks is ideal for A/B testing or feature flags. Register hooks conditionally based on user segments or feature toggles (e.g., via middleware or Laravel events). For example, inject a new header variant for a subset of users without modifying the base template. Combine with Laravel’s `feature()` helper for dynamic hook activation.
Are there any known conflicts with other Twig extensions or Laravel packages?
Conflicts are rare but possible if another Twig extension uses the same hook names or modifies the Twig environment. Always check for namespace collisions and ensure your Twig extensions are properly isolated. Test with a minimal setup to verify compatibility, especially if using packages like `spatie/laravel-twig` or custom Twig extensions.
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.
calmfox/watch-sylius
damienfern/grpc-symfony-bundle
atoolo/index-bundle
atoolo/genai-bundle
coprotoai/laravel-ticket
davidjln/llm-carbon-bundle
cryonighter/valid-request-bundle
coolms/taxonomy-bundle
coolms/field-bundle
articulate-orm/symfony
aaix/laravel-tall-architect
ephoto/akeneo-connector
emmanuelballery/eb-plantumlbundle
emielburgman/symfony-visitor-beacon
emielburgman/symfony-visit-storage
emielburgman/symfony-security-headers
emielburgman/symfony-log-viewer
emarref/xdebug-bundle
emarref/pubnub-bundle
elriseio/finance-money-bundle