I'm trying to require a .js.php file in codesleeve/asset-pipeline for Laravel 4, and have not been able to get it working successfully. Has anyone else had trouble with this?
The problem...
I use gettext to feed some translation strings into JavaScript (e.g. for error messages). My javascript looks something like this (locale.js.php) where _e
is an escaped string from gettext using sprint.
Locale.define('<?php echo $USER_LOCALE; ?>', 'Number' , {
billion: '<?php echo _e('%s billion', '{x}'); ?>'
});
This all works perfectly when throwing the correct header on it and calling the file directly. It will output:
Locale.define('fr-FR', 'Number' , {
billion: 'milliard {x}'
});
In my asset-pipeline config file, I added the extension .js.php
to my javascripts mime array, and added the filter:
'filters' => array(
'.js.php' => array(
new AssetPipelinePHP
),
...
where the class AssetPipelinePHP is: (I know eval
is dangerous, but bear with me)
class AssetPipelinePHP implements FilterInterface {
...
public function filterDump(AssetInterface $asset) {
$content = eval("?> " . $asset->getContent() . " <?php ");
$asset->setContent($content);
}
}
Finally, in my application.js file I have:
//= require_tree modernizr
//= require_tree mootools
//= require_tree locale
This will display the javascript correctly (though ABOVE modernizr instead of at the end of the file) for the first page load only. Consecutive page loads will not display any of the locale.js.php file. However, if I change the locale.js.php file at all (inserting a will do) then again it shows correctly for one page load.