3

I would like to avoid browser cache on my images by appending the SVN revision number after each images like this (in the same fashion than this answser):

<?php $v = getRevisionNumber() ?>
<img src="picture.jpg?v=<?= $v ?>" alt="">

Is there a way to do it automatically in Symfony 1.4 (like this for js/css, but with images instead)

Also, how can I do it for an image that is in a css file ?

#title {
    background-image: url(/images/title.png);
}
Community
  • 1
  • 1
Benjamin Crouzier
  • 34,915
  • 36
  • 154
  • 219
  • Hmm... overriding the `image_path()` in the `AssetHelper` would what you're asking for those called from your template files. Are you asking for a config in some yml file setting somewhere to that will append a suffix like you're asking? The Css though I'm not sure. May have to add your own custom parsing. image_path source: http://trac.symfony-project.org/browser/branches/1.4/lib/helper/AssetHelper.php#L299 – Yzmir Ramirez Mar 18 '12 at 02:08
  • What do you mean by `May have to add your own custom parsing.` ? What I would like is to append a parameter on every image (called with image_helper or without php, as in the css example I gave) . – Benjamin Crouzier Mar 18 '12 at 11:49
  • Well you can dynamically generate your CSS with a PHP script, rendering a page with `header('Content-type: text/css')`, would probably be how you would do it. You could, with this script, read the CSS and then parse and filter it with your requirements. Perhaps someone else could provide a better solution? +1 Good question – Yzmir Ramirez Mar 18 '12 at 19:55
  • @YzmirRamirez My project is quite big, that would mean renaming quite me lot of css files and changing a lot of images paths. That's what I am trying to avoid – Benjamin Crouzier Mar 18 '12 at 20:12

1 Answers1

1

I found something interesting in the symfony tracker, for the version 1.3/1.4, there were a patch to automatically add a timestamp to all files in the web directory: http://trac.symfony-project.org/ticket/6135

It has been reverted since, no idea why ... (to intrusive?).

Override the default asset helper

Anyway, I think you have to create your own AssetHelper (copied all contents from the current one) and add & customize the patch #6135 into a lib/helper/CustomAssetHelper.php.

But you can't unload the AssetHelper because it is automatically loaded in the core: http://trac.symfony-project.org/browser/branches/1.4/lib/view/sfPHPView.class.php#L33 So there will be conflict since you will have duplicate function (in AssetHelper and CustomAssetHelper).

Add a custom template engine

The idea is to have a custom sfPHPView to redefine the loadCoreAndStandardHelpers to call your own asset helper (put it in lib/view/sfCustomPHPView.class.php):

class sfCustomPHPView extends sfPHPView
{
  /**
   * Loads core and standard helpers to be use in the template.
   */
  protected function loadCoreAndStandardHelpers()
  {
    static $coreHelpersLoaded = 0;

    if ($coreHelpersLoaded)
    {
      return;
    }

    $coreHelpersLoaded = 1;

    $helpers = array_unique(array_merge(array('Helper', 'Url', 'CustomAsset', 'Tag', 'Escaping'), sfConfig::get('sf_standard_helpers')));

    // remove default Form helper if compat_10 is false
    if (!sfConfig::get('sf_compat_10') && false !== $i = array_search('Form', $helpers))
    {
      unset($helpers[$i]);
    }

    $this->context->getConfiguration()->loadHelpers($helpers);
  }
}

To change the default sfPHPView, you need to add a module.yml in config/ or apps/frontend/config/ with the following contents (inspired from sfTwigPlugin):

all:
  view_class: sfCustom

Override all image_tag()

As Yzmir Ramirez said, image_tag() calls image_path() which call _compute_public_path($source, 'images', 'png', $absolute);.

In _compute_public_path function, before the last condition, you customize the query_string to add your own revision number (which will be define somewhere else - sfConfig for example):

$file = sfConfig::get('sf_web_dir').$source;
if ('images' == $dir && sfConfig::get('my_revision_number'))
{
  $query_string .= sfConfig::get('my_revision_number');
}

It might be a bit complex but using this way, you can override image_tag function and add the version number you want without redefine all you image_tag() call.

About image inside a CSS, it's a bit more complex since you will have to parse css or write css in PHP. No idea about the best way to do.

j0k
  • 21,914
  • 28
  • 75
  • 84