Skip to content

Instantly share code, notes, and snippets.

@mattattui
Created September 13, 2012 11:50
Show Gist options
  • Save mattattui/3713822 to your computer and use it in GitHub Desktop.
Save mattattui/3713822 to your computer and use it in GitHub Desktop.
Symfony 1.4 versioned stylesheets/css
app:
# ...
resource_version: <?php echo date_create()->format('YmdHis');
<?php
function include_stylesheets_versioned()
{
$response = sfContext::getInstance()->getResponse();
sfConfig::set('symfony.asset.stylesheets_included', true);
$html = '';
foreach ($response->getStylesheets() as $file => $options) {
if ((strpos($file, '?') === false) && (stripos($file, 'http') !== 0) ) {
$file .= '?v='.sfConfig::get('app_resource_version');
}
$html .= stylesheet_tag($file, $options);
}
echo $html;
}
function include_javascripts_versioned()
{
$response = sfContext::getInstance()->getResponse();
sfConfig::set('symfony.asset.javascripts_included', true);
$html = '';
foreach ($response->getJavascripts() as $file => $options) {
if ((strpos($file, '?') === false) && (stripos($file, 'http') !== 0) ) {
$file .= '?v='.sfConfig::get('app_resource_version');
}
$html .= javascript_include_tag($file, $options);
}
echo $html;
}
@mattattui
Copy link
Author

This automatically busts browser caches for CSS & JS files when you clear your Symfony 1.4 application's cache. This doesn't always make sense.

To use it, put the helper into lib/helper (create the folder if necessary), then add it to the default_modules section in your apps/[APPNAME]/config/settings.yml, and finally replace the include_stylesheets() and include_javascripts() calls in your layout.php with the _versioned() calls.

You can't embed PHP into YAML files in Symfony2, but then you're using Assetic anyway, right?

@edriang
Copy link

edriang commented Jun 8, 2016

Excellent Helper! I'll add some clarification about how to implement this for other devs.

In settings.yml you have to configure standard_helpers, not default_modules, like ie:
standard_helpers: [Partial, Cache, glxAsset]
You want to include Partial and Cache, which are the default ones

If you like to do this in a "by template" fashion, you can add this line at the beginning of your layout.php:
<?php use_helper('glxAsset') ?>

How this tips helps others..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment