Created
September 13, 2012 11:50
-
-
Save mattattui/3713822 to your computer and use it in GitHub Desktop.
Symfony 1.4 versioned stylesheets/css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app: | |
# ... | |
resource_version: <?php echo date_create()->format('YmdHis'); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
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
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 thedefault_modules
section in yourapps/[APPNAME]/config/settings.yml
, and finally replace theinclude_stylesheets()
andinclude_javascripts()
calls in yourlayout.php
with the_versioned()
calls.You can't embed PHP into YAML files in Symfony2, but then you're using Assetic anyway, right?