appventus/assetic-injector-bundle
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.)
Enable the Bundle in config/bundles.php:
return [
// ...
Troopers\AsseticInjectorBundle\TroopersAsseticInjectorBundle::class => ['all' => true],
];
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' }
Use in Templates (Twig):
{{ assetic_injector_global_stylesheets() }}
{{ assetic_injector_global_javascripts() }}
(Place these in your base template, e.g., base.html.twig.)
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:
<head> and before </body>, respectively.Modular Asset Management:
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' }
{% if app.user.isAdmin %}
{{ assetic_injector_admin_stylesheets() }}
{% endif %}
Dynamic Asset Loading:
{% if app.request.attributes.get('_route') == 'homepage' %}
{{ assetic_injector_homepage_scripts() }}
{% endif %}
Asset Prioritization:
With Symfony Assetic:
# config/packages/assetic.yaml
assetic:
filters:
cssrewrite: ~
uglifyjs2:
bin: "/usr/bin/nodejs %s"
Environment-Specific Assets:
%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.)
Custom Asset Types:
troopers_assetic_injector:
global_javascripts:
- { path: 'https://cdn.example.com/script.js', type: 'remote' }
(Requires bundle customization; see "Extension Points" below.)
Deprecated Package:
appventus/assetic-injector-bundle is archived. Use troopers/assetic-injector-bundle (MIT-licensed) instead.AppVentus\AsseticInjectorBundle namespace references in your code with Troopers\AsseticInjectorBundle.No Built-in Asset Versioning:
<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' }
Twig Function Overrides:
assetic_injector_global_stylesheets(), ensure the parent function is called to avoid breaking inheritance:
{% block stylesheets %}
{{ parent() }}
{{ assetic_injector_custom_stylesheets() }}
{% endblock %}
Asset Path Resolution:
public/. Use absolute paths (e.g., bundles/yourbundle/...) or configure a base path in the bundle’s config.Missing Assets:
public/./_profiler) for Assetic errors or missing files.Duplicate Injections:
{{ dump(assetic_injector_global_stylesheets()) }} to inspect generated HTML and debug duplicates.Environment-Specific Issues:
php bin/console cache:clear
Extension Points:
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>';
}
}
config/packages/twig.yaml:
twig:
extensions:
- App\Twig\Extension\CustomAsseticInjectorExtension
Performance:
<body> by splitting YAML into head_javascripts and body_javascripts keys.Testing:
$twig = $this->getMockBuilder('Twig_Environment')
->disableOriginalConstructor()
->getMock();
$twig->method('render')
->with('assetic_injector_global_stylesheets')
->willReturn('<link rel="stylesheet" href="/css/global.css">');
How can I help you explore Laravel packages today?