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

Static Container Twig Extension Bundle Laravel Package

danilovl/static-container-twig-extension-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require danilovl/static-container-twig-extension-bundle
    

    Ensure bundles.php includes the bundle (auto-registered in Symfony 8+).

  2. First Use Case: Store data in a template and retrieve it in another:

    {# templates/parent.html.twig #}
    {{ static_container.set('user_id', app.user.id) }}
    
    {# templates/child.html.twig #}
    {{ static_container.get('user_id') }}
    

Key Locations

  • Twig Functions: static_container.set(), static_container.get(), static_container.has().
  • Configuration: Minimal—no extra config needed beyond bundle registration.

Implementation Patterns

Workflows

  1. Global State Management: Use static_container.set() in a parent template (e.g., base.html.twig) to pass data to child templates (e.g., modals, partials).

    {# base.html.twig #}
    {{ static_container.set('theme_color', '#3498db') }}
    
  2. Conditional Data Sharing: Check existence before retrieval:

    {% if static_container.has('user_id') %}
        {{ static_container.get('user_id') }}
    {% endif %}
    
  3. Integration with Controllers: Pre-populate the container via a Twig extension or event listener:

    // src/EventListener/StaticContainerListener.php
    public function onKernelView(GET $event): void
    {
        $container = $event->getContainer();
        $twig = $container->get('twig');
        $twig->addGlobal('static_container', $container->get('static_container_twig_extension'));
    }
    

Integration Tips

  • Avoid Overuse: Limit to truly global data (e.g., user preferences, theme settings). Prefer dependency injection for component-specific data.
  • Type Safety: Cast values explicitly in Twig:
    {{ static_container.get('user_id')|int }}
    
  • Clear Data: Use static_container.clear() in logout routes or template teardowns.

Gotchas and Tips

Pitfalls

  1. Thread Safety: The container is not thread-safe. Avoid concurrent writes in multi-threaded environments (e.g., Symfony Messenger workers).

  2. Memory Leaks: Uncleared data persists across requests. Clear explicitly in critical paths:

    {{ static_container.clear() }} {# After sensitive operations #}
    
  3. Twig Debugging: Overwritten keys silently replace old values. Use static_container.has() to verify:

    {% if not static_container.has('key') %}
        {{ static_container.set('key', default_value) }}
    {% endif %}
    

Debugging

  • Inspect Contents: Dump the container in a template:
    {{ dump(static_container.getAll()) }}
    
  • Check Registration: Verify the bundle is loaded via:
    php bin/console debug:container static_container_twig_extension
    

Extension Points

  1. Custom Storage: Override the default storage backend by implementing StaticContainerInterface and binding it in services.yaml:

    services:
        App\Service\CustomStaticContainer:
            tags: ['static_container.storage']
    
  2. Event-Driven Clearing: Listen to kernel events to auto-clear data:

    // src/EventListener/ClearStaticContainerListener.php
    public function onKernelTerminate(RequestEvent $event): void
    {
        $container = $event->getContainer();
        $container->get('static_container_twig_extension')->clear();
    }
    
  3. Serialization: For complex objects, implement __toString() or use serialize():

    {{ static_container.set('user', serialize(app.user)) }}
    {{ static_container.get('user')|unserialize }}
    
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.
nasirkhan/laravel-sharekit
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony