Installation
Add the bundle to composer.json:
composer require dlancea/coffee-php-bundle:dev-master
Register in AppKernel.php:
new DLancea\CoffeePhpBundle\DLanceaCoffeePhpBundle(),
First Use Case
Create a .coffee file (e.g., app.coffee) in your Resources/assets/coffee/ directory.
Reference it in Twig with the coffeephp filter:
{% javascripts filter='coffeephp' '@AcmeBundle/Resources/assets/coffee/app.coffee' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
Clear cache:
php bin/console cache:clear --env=prod
Asset Organization
.coffee files in a dedicated directory (e.g., Resources/assets/coffee/).javascripts tag with the coffeephp filter to compile and bundle them.Twig Integration
{% javascripts filter='coffeephp' '@AcmeBundle/Resources/assets/coffee/module.coffee' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
{% javascripts
filter='coffeephp'
'@AcmeBundle/Resources/assets/coffee/base.coffee'
'@AcmeBundle/Resources/assets/coffee/extension.coffee'
%}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
Debugging Mode
dev environment, Assetic generates separate files for each .coffee input (useful for debugging).{% javascripts ... %} without filter in dev to verify raw .coffee loading.Custom Compilation Logic
Cache Dependencies
php bin/console cache:clear) after adding/updating .coffee files will result in stale JavaScript output.--env=prod explicitly for production cache clearing.Assetic Configuration Conflicts
debug or use_controller settings), the coffeephp filter may fail silently.config.yml:
assetic:
debug: "%kernel.debug%"
use_controller: false
File Permissions
app/cache/{env}/assetic/. Ensure the web server user has write permissions.chmod -R 775 app/cache.CoffeeScript Syntax Errors
.coffee files using an online compiler (e.g., CoffeeScript REPL) before integrating.Inspect Compiled Output
app/cache/{env}/assetic/ to verify compilation.Log Filter Output
debug: true in config.yml) to log filter execution:
assetic:
debug: true
Fallback for Complex Projects
.coffee files to .js in a build step (e.g., GitHub Actions)..js files in production.Custom Compiler Options
coffeescript-php library supports options like bare: true (remove wrapper function).DLancea\CoffeePhpBundle\Assetic\Filter\CoffeePhpFilter and override compile():
class CustomCoffeePhpFilter extends CoffeePhpFilter {
protected function compile($code) {
return parent::compile($code, ['bare' => true]);
}
}
services.yml:
services:
custom.coffeephp.filter:
class: AppBundle\Assetic\Filter\CustomCoffeePhpFilter
tags:
- { name: assetic.filter, alias: coffeephp }
Pre/Post-Processing
coffeephp filter with others (e.g., uglifyjs) for minification:
{% javascripts filter='coffeephp,uglifyjs' '@AcmeBundle/Resources/assets/coffee/app.coffee' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
How can I help you explore Laravel packages today?