braunstetter/assets-push-bundle
Installation:
composer require braunstetter/assets-push-bundle
Add to config/bundles.php:
return [
// ...
Braunstetter\AssetsPushBundle\AssetsPushBundle::class => ['all' => true],
];
First Use Case:
In a Twig template (e.g., templates/base.html.twig), register assets:
{% css '/path/to/file.css' %}
{% js '/path/to/script.js' %}
Then, in your layout or base template, render them in <head>:
{% block pushedJs %}
{% for js in assets()['js'] %}
<script src="{{ asset(js) }}"></script>
{% endfor %}
{% endblock %}
{% css %} and {% js %} tags for asset registration.assets() Function: Use this in your base template to render collected assets.config/packages/braunstetter_assets_push.yaml (if auto-generated) for customization.Component-Based Asset Management:
components/breadcrumb.html.twig):
{% css 'bundles/app/css/breadcrumb.css' %}
{% js 'bundles/app/js/breadcrumb.js' %}
assets().Dynamic Asset Loading:
assets() in controllers to conditionally push assets:
return $this->render('page.html.twig', [
'extra_css' => ['/dynamic.css'],
'extra_js' => ['/dynamic.js'],
]);
{% for css in extra_css %}
{% css css %}
{% endfor %}
Base Template Integration:
pushedJs and pushedCss blocks in child templates:
{% block pushedJs %}
{{ parent() }}
<script src="{{ asset('/child.js') }}"></script>
{% endblock %}
?v={{ env('APP_VERSION') }} to paths to bust caches:
{% css '/file.css?v=' ~ env('APP_VERSION') %}
if to load assets only when needed:
{% if is_feature_enabled('analytics') %}
{% js '/analytics.js' %}
{% endif %}
{% js '/stimulus.js' %}
{% js '/controllers/app_controller.js' %}
Double Registration:
{% block %} to extend instead of re-pushing:
{% block pushedJs %}
{{ parent() }} {# Renders parent assets first #}
<script src="{{ asset('/child.js') }}"></script>
{% endblock %}
Asset Path Resolution:
public/ by default. Use absolute paths (e.g., bundles/app/css/file.css) or asset() helper:
{% css asset('css/file.css') %}
Twig Cache Invalidation:
php bin/console cache:clear --env=prod
assets() in a template to verify registration:
{{ dump(assets()) }}
{% css %} and {% js %} tags are closed properly (no trailing spaces or line breaks).Custom Asset Types:
Override the bundle’s AssetCollector service to support additional types (e.g., {% font %}):
# config/services.yaml
Braunstetter\AssetsPushBundle\AssetCollector:
arguments:
$supportedTypes: ['css', 'js', 'font']
Pre/Post-Processing:
Extend the AssetCollector to modify paths or add attributes:
// src/EventSubscriber/AssetsSubscriber.php
public function onKernelRequest(GetResponseEvent $event)
{
$assets = $event->getRequest()->attributes->get('assets', []);
$assets['js'] = array_map(fn($path) => $path . '?defer', $assets['js']);
$event->getRequest()->attributes->set('assets', $assets);
}
Environment-Specific Assets:
Use Twig’s {% if app.environment == 'dev' %} to conditionally push assets:
{% if app.environment == 'dev' %}
{% css '/dev-toolbar.css' %}
{% endif %}
assets_push_enabled: false in Twig’s context:
{% set _context.get('assets_push_enabled') = false %}
pushedJs, pushedCss) in config/packages/braunstetter_assets_push.yaml:
assets_push:
js_block: 'custom_js'
css_block: 'custom_css'
How can I help you explore Laravel packages today?