asoc/assetic-angular-js-bundle
Installation:
composer require asoc/assetic-angular-js-bundle
Ensure your project uses Symfony 2.3+ and AsseticBundle (symfony/assetic-bundle:^2.3).
First Use Case:
Add AngularJS templates to your app/config/config.yml under assetic:
assetic:
filters:
angular: ~
Then, in a Twig template:
{% javascripts filter="angular"
'path/to/templates/*.html.ng'
%}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
Verify Output:
Check the generated JS to confirm templates are cached in Angular’s $templateCache.
Template Organization:
.html.ng (or custom extension) for Angular templates.Resources/views/auth/*.html.ng).Dynamic Template Loading:
data-ng-include:
<div data-ng-include="'Auth/login.html'"></div>
put() key in the generated JS (e.g., Auth/login.html).Asset Management:
angular filter.uglifyjs or yui_css if needed:
{% javascripts filter="angular,uglifyjs" %}
Module-Specific Templates:
Auth/) to avoid collisions.angular.module('Auth.templates', []).requires.push('Auth');
Environment-Specific Config:
app_dev.php checks).Custom Path Formatting:
Override the default path formatter by extending the bundle’s TemplateNameFormatter service.
Example:
// src/Acme/AppBundle/DependencyInjection/Compiler/TemplateNameFormatterPass.php
public function process(ContainerBuilder $container) {
$definition = $container->findDefinition('asoc_assetic_angular_js.template_name_formatter');
$definition->setArgument(0, function($formatter) {
return function($path) {
return str_replace(['Resources/views/', '.html.ng'], '', $path);
};
});
}
Lazy-Loading Templates:
Use ngRoute or ui-router to load templates dynamically, bypassing the cache for specific cases.
Path Resolution Issues:
asset() helper or absolute paths).$templateCache are case-sensitive (e.g., Auth/Login.html ≠ auth/login.html).Caching Conflicts:
php app/console assetic:dump --env=prod --no-debug
<script src="{{ asset_url }}?v=1.0"></script>
Angular Module Dependencies:
angular.module('App.templates', []); // Required!
HTML Escaping:
Inspect Generated JS:
Open the browser’s dev tools to verify template paths and content in $templateCache.
Example output:
$templateCache.put("Auth/login.html", "<div>{{ form }}</div>");
Log Filter Output: Temporarily add a debug dump in a custom filter:
// src/Acme/AppBundle/Twig/Extension/DebugExtension.php
public function getTemplateCacheDump() {
return $this->container->get('assetic.assets')->get('your_template_asset')->getContent();
}
Check Assetic Configuration:
Validate the angular filter is registered in config.yml:
assetic:
filters:
angular: ~
Custom Filters:
Extend the AngularJsFilter class to modify template processing (e.g., minification, pre-processing).
Template Preprocessing:
Use Assetic’s raw filter to pre-process templates (e.g., with PHP includes):
{% javascripts filter="raw,angular" %}
{% include 'partials/header.html.twig' %}
{% endjavascripts %}
Dynamic Module Names: Override the module name logic to use a dynamic namespace (e.g., based on route or user role).
Integration with Webpack/Encore:
If migrating from Assetic, use encore.js to generate the template cache via a custom loader:
// webpack.config.js
Encore
.addEntry('templates', './assets/templates/*.html')
.copyFiles({
from: './assets/templates',
to: 'templates/[path][name].html',
});
How can I help you explore Laravel packages today?