Installation:
composer require bmatzner/underscore-bundle:~1.8
Update AppKernel.php to include the bundle:
new Bmatzner\UnderscoreBundle\BmatznerUnderscoreBundle(),
Asset Installation:
php app/console assets:install web --symlink
First Use Case: Include the library in a Twig template:
<script src="{{ asset('bundles/bmatznerunderscore/js/underscore.min.js') }}"></script>
Use Underscore.js in a frontend script:
<script>
_.each([1, 2, 3], function(num) { console.log(num); });
</script>
vendor/bmatzner/underscore-bundle/ for source files.app/console assets:install for asset handling.Frontend Utility Library: Use Underscore.js for client-side data manipulation (e.g., filtering, mapping, templating). Example:
// Filter an array of objects
var filtered = _.filter(users, { age: 25 });
Twig Integration: Pass data from PHP to Twig, then process it with Underscore in the browser:
<script>
var userData = {{ users|json_encode|raw }};
var activeUsers = _.filter(userData, { active: true });
</script>
Custom Templates: Use Underscore’s templating engine for dynamic HTML:
var template = _.template('<%= name %> is <%= age %> years old.');
template({ name: 'Alice', age: 30 });
Event Delegation: Leverage Underscore for lightweight event handling:
_.extend($, {
delegate: function(selector, event, handler) {
$(document).on(event, selector, handler);
}
});
Asset Optimization:
Combine underscore.min.js with other JS files using Symfony’s assetic or webpack-encore.
Example webpack.config.js:
Encore
.addEntry('app', './assets/app.js')
.addEntry('underscore', 'bmatzner/underscore-bundle/js/underscore.min.js')
.splitEntry('underscore');
Versioning:
Pin the bundle version in composer.json to avoid breaking changes:
"bmatzner/underscore-bundle": "1.8.0"
Testing: Use Jasmine or Mocha with Underscore for frontend unit tests.
Asset Paths:
bundles/bmatznerunderscore/js/underscore.min.js may break if the bundle is renamed or moved.{{ asset() }} in Twig.Bundle Deprecation:
Symfony 4+ Compatibility:
npm install underscore --save
Then import in webpack.config.js:
Encore.enableSingleRuntimeChunk();
Encore.addEntry('underscore', 'underscore');
License Conflicts:
Missing Library:
Verify assets are installed (web/bundles/bmatznerunderscore/js/underscore.min.js exists).
Clear cache if needed:
php app/console cache:clear
Underscore Undefined:
Check for JavaScript errors (e.g., ReferenceError: _ is not defined). Ensure the script tag loads after DOM ready:
<script src="{{ asset('bundles/bmatznerunderscore/js/underscore.min.js') }}"></script>
<script src="{{ asset('js/app.js') }}"></script>
Custom Builds:
Override the default underscore.min.js by copying the file to your project’s web/bundles/ and modifying it.
Symfony Integration: Create a Twig extension to expose Underscore utilities to templates:
// src/Twig/UnderscoreExtension.php
class UnderscoreExtension extends \Twig_Extension
{
public function getFunctions()
{
return [
new \Twig_SimpleFunction('underscore_filter', function($array, $predicate) {
return json_encode(_.filter($array, $predicate));
}),
];
}
}
Usage in Twig:
{{ underscore_filter(users, { active: true })|raw }}
Lodash Migration: Replace Underscore with Lodash by:
npm install lodash --save
Lazy Loading: Load Underscore asynchronously if not needed immediately:
<script src="{{ asset('bundles/bmatznerunderscore/js/underscore.min.js') }}" defer></script>
Tree Shaking: If using Webpack, configure it to exclude unused Underscore methods:
Encore.configureBabel((config) => {
config.plugins.push('@babel/plugin-transform-runtime');
});
How can I help you explore Laravel packages today?