smoya/asset-management-bundle
Symfony2 bundle to manage Assetic asset inclusion in Twig. Add assets from any template with assets_add() and render them later with assets_render() (e.g., collect JS/CSS in child templates and output in a base layout where you want).
Installation
Add the bundle to your composer.json:
composer require smoya/asset-management-bundle
Enable it in config/bundles.php:
return [
// ...
Smoya\AssetManagementBundle\SmoyaAssetManagementBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console smoya:asset-management:install
Edit config/packages/smoya_asset_management.yaml to define your asset groups (e.g., vendor, app, admin).
First Use Case Define an asset group in Twig:
{% asset_group 'app' %}
<!-- Include CSS/JS here -->
{{ asset('css/app.css') }}
{{ asset('js/app.js') }}
{% endasset_group %}
The bundle auto-generates a unique hash for the group and bundles assets with Assetic.
Define Groups
Use asset_group in Twig to scope assets (e.g., frontend, backend, third-party):
{% asset_group 'frontend' %}
{{ asset('css/bootstrap.min.css') }}
{{ asset('js/main.js') }}
{% endasset_group %}
Dynamic Grouping Pass variables to Twig for conditional inclusion:
{% if is_admin() %}
{% asset_group 'admin' %}
{{ asset('css/admin.css') }}
{% endasset_group %}
{% endif %}
Integration with Assetic
Configure assetic.yaml to process grouped assets:
assets:
app_css:
inputs:
- '%kernel.project_dir%/assets/css/app.css'
filters: [cssrewrite, yui_css]
Reference the group in Twig:
{{ asset_group('app_css')|assetic_filter }}
<link href="{{ path(asset_group('app'), {'hash': true}) }}" rel="stylesheet">
%kernel.environment% to toggle groups:
# config/packages/smoya_asset_management.yaml
groups:
app:
enabled: ['dev', 'prod']
{% asset_group 'app' %}
{{ asset('js/vendor.js')|assetic_filter('uglifyjs') }}
{% endasset_group %}
asset_group_preload Twig function for critical resources:
{% asset_group_preload 'app' %}
Missing Config
No asset groups defined.php bin/console smoya:asset-management:install and verify config/packages/smoya_asset_management.yaml.Assetic Not Processed
/assets/css/app.css).assetic:dump is run in dev environment:
php bin/console assetic:dump --env=dev
Group Name Collisions
frontend_v2, admin_dashboard).{{ dump(asset_group('app')) }}
php bin/console cache:clear
smoya_asset_management.yaml:
groups:
analytics:
enabled: ['prod']
asset() calls to avoid resolution issues:
{{ asset('@assets/css/app.css') }} {# Webpack Encore style #}
Custom Asset Group Logic
Override the bundle’s AssetGroupManager to add logic (e.g., dynamic group names):
// src/SmoyaAssetManagement/AssetGroupManager.php
class CustomAssetGroupManager extends \Smoya\AssetManagementBundle\AssetGroupManager
{
public function getGroupName($name) {
return 'custom_' . $name;
}
}
Register the service in services.yaml:
Smoya\AssetManagementBundle\AssetGroupManager: '@custom_asset_group_manager'
Twig Extensions Extend the Twig environment to add custom functions:
// src/Twig/AppExtension.php
class AppExtension extends \Twig\Extension\AbstractExtension
{
public function getFunctions() {
return [
new \Twig\TwigFunction('custom_asset_group', [$this->manager, 'renderGroup']),
];
}
}
How can I help you explore Laravel packages today?