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

Assetic Injector Bundle Laravel Package

appventus/assetic-injector-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install the Package (now maintained by Troopers):

    composer require troopers/assetic-injector-bundle
    

    (Note: Redirect from appventus/assetic-injector-bundle to troopers/assetic-injector-bundle per README.)

  2. Enable the Bundle in config/bundles.php:

    return [
        // ...
        Troopers\AsseticInjectorBundle\TroopersAsseticInjectorBundle::class => ['all' => true],
    ];
    
  3. Configure Global Assets in config/packages/troopers_assetic_injector.yaml:

    troopers_assetic_injector:
        global_stylesheets:
            - { path: 'bundles/yourbundle/css/global.css' }
        global_javascripts:
            - { path: 'bundles/yourbundle/js/global.js' }
    
  4. Use in Templates (Twig):

    {{ assetic_injector_global_stylesheets() }}
    {{ assetic_injector_global_javascripts() }}
    

    (Place these in your base template, e.g., base.html.twig.)


First Use Case

Problem: You need to inject global CSS/JS (e.g., a site-wide analytics script or a base stylesheet) into every page without manually adding them to every template.

Solution:

  1. Define the assets in the YAML config.
  2. Include the Twig functions in your layout.
  3. The bundle automatically injects them into the <head> and before </body>, respectively.

Implementation Patterns

Workflows

  1. Modular Asset Management:

    • Group assets by feature (e.g., admin_stylesheets, frontend_javascripts) using separate YAML keys:
      troopers_assetic_injector:
          admin_stylesheets:
              - { path: 'bundles/admin/css/dashboard.css' }
          frontend_javascripts:
              - { path: 'bundles/frontend/js/analytics.js' }
      
    • Conditionally inject them in Twig:
      {% if app.user.isAdmin %}
          {{ assetic_injector_admin_stylesheets() }}
      {% endif %}
      
  2. Dynamic Asset Loading:

    • Use Twig logic to load assets based on routes or user roles:
      {% if app.request.attributes.get('_route') == 'homepage' %}
          {{ assetic_injector_homepage_scripts() }}
      {% endif %}
      
  3. Asset Prioritization:

    • Order matters in the YAML array. Place critical assets (e.g., polyfills) first.

Integration Tips

  1. With Symfony Assetic:

    • Ensure Assetic is configured to process these assets (e.g., minification, versioning):
      # config/packages/assetic.yaml
      assetic:
          filters:
              cssrewrite: ~
              uglifyjs2:
                  bin: "/usr/bin/nodejs %s"
      
  2. Environment-Specific Assets:

    • Use %kernel.environment% in YAML to load dev-only assets:
      troopers_assetic_injector:
          global_javascripts:
              - { path: 'bundles/dev/js/dev-toolbar.js', env: 'dev' }
      

    (Note: The bundle may not natively support env filtering; check Troopers' docs for custom extensions.)

  3. Custom Asset Types:

    • Extend the bundle to support inline scripts/styles or remote URLs:
      troopers_assetic_injector:
          global_javascripts:
              - { path: 'https://cdn.example.com/script.js', type: 'remote' }
      

    (Requires bundle customization; see "Extension Points" below.)


Gotchas and Tips

Pitfalls

  1. Deprecated Package:

    • The original appventus/assetic-injector-bundle is archived. Use troopers/assetic-injector-bundle (MIT-licensed) instead.
    • Migration Tip: Replace AppVentus\AsseticInjectorBundle namespace references in your code with Troopers\AsseticInjectorBundle.
  2. No Built-in Asset Versioning:

    • The bundle injects raw <link>/<script> tags. For cache-busting, prepend hashes to asset paths in YAML or use Assetic’s version filter:
      global_stylesheets:
          - { path: 'bundles/yourbundle/css/global.css', version: '1.0' }
      
  3. Twig Function Overrides:

    • If you override assetic_injector_global_stylesheets(), ensure the parent function is called to avoid breaking inheritance:
      {% block stylesheets %}
          {{ parent() }}
          {{ assetic_injector_custom_stylesheets() }}
      {% endblock %}
      
  4. Asset Path Resolution:

    • Paths in YAML are resolved relative to public/. Use absolute paths (e.g., bundles/yourbundle/...) or configure a base path in the bundle’s config.

Debugging

  1. Missing Assets:

    • Verify paths in YAML exist in public/.
    • Check Symfony’s profiler (/_profiler) for Assetic errors or missing files.
  2. Duplicate Injections:

    • Use {{ dump(assetic_injector_global_stylesheets()) }} to inspect generated HTML and debug duplicates.
  3. Environment-Specific Issues:

    • Clear cache after changing YAML:
      php bin/console cache:clear
      

Tips

  1. Extension Points:

    • Override the bundle’s services to add custom logic. Example: Extend Troopers\AsseticInjectorBundle\Twig\AsseticInjectorExtension to support inline assets:
      // src/Twig/Extension/CustomAsseticInjectorExtension.php
      class CustomAsseticInjectorExtension extends \Twig_Extension
      {
          public function getFunctions()
          {
              return [
                  new \Twig_SimpleFunction('assetic_injector_inline_script', [$this, 'injectInlineScript']),
              ];
          }
      
          public function injectInlineScript($script)
          {
              return '<script>' . $script . '</script>';
          }
      }
      
    • Register the extension in config/packages/twig.yaml:
      twig:
          extensions:
              - App\Twig\Extension\CustomAsseticInjectorExtension
      
  2. Performance:

    • Defer non-critical global JS to the end of <body> by splitting YAML into head_javascripts and body_javascripts keys.
  3. Testing:

    • Mock the Twig functions in PHPUnit:
      $twig = $this->getMockBuilder('Twig_Environment')
          ->disableOriginalConstructor()
          ->getMock();
      $twig->method('render')
           ->with('assetic_injector_global_stylesheets')
           ->willReturn('<link rel="stylesheet" href="/css/global.css">');
      
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.
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon
itsemon245/lamet
baks-dev/dashboard
amoifr/pickle-panther-bundle
make-dev/orca
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle