csssmartyversioningsmarty2

Automate the css versioning in smarty(2.6)


I need to force CSS changes to go live immediately and I found that add a version to CSS would help to do that as bellow.

<link rel="stylesheet" href="css/style.css?version=123456" media="all"/>

I need to automate this as it is difficult to change the main file always when need to do small change to css file.

So I found following line of code(sample) in PHP doing the same job.

<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); echo '?' . filemtime( get_stylesheet_directory() . '/style.css'); ?>" type="text/css" media="screen, projection" />

I have tried to convert this line to Smarty, but it is giving error.

code :

 <link rel="stylesheet" href="css/style.css?version={@filemtime:css/style.css} />

error:

syntax error: unrecognized tag: @filemtime:...........

Anybody has an idea how to do this ?

Thanks in advance


Solution

  • You can add this modifier to smarty plugin folder,

    function smarty_modifier_filemtime($path){
        $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
        return filemtime( YOUR_HOME_DIR.$path );
    }
    

    then call it like bellow,

    <link rel="stylesheet" href="/css/style.css?v={'/css/style.css'|@filemtime}" type="text/css">