Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

how to replace "echo filemtime('filename.css')" wi

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
bg17aw
Smarty n00b


Joined: 14 Nov 2016
Posts: 3

PostPosted: Wed Nov 16, 2016 7:28 pm    Post subject: how to replace "echo filemtime('filename.css')" wi Reply with quote

In the header of my template, I add a random string to force load of css and Javascript files:
Code:

<link rel="stylesheet" href="/default.css?<?php echo filemtime('/default.css') ?>" type="text/css" >
<script src="js/main.js?<?php echo filemtime('js/main.js') ?>"></script>


However, no idea how to achieve the same thing in Smarty.

Any help to achieve THE EXACT same thing?
I found a solution about declaring a smarty variable with a random value, but the above code is a more elegant solution, so I would like to keep it.

Thanks!
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Nov 16, 2016 9:23 pm    Post subject: Reply with quote

That's hardly a random value, and you, indeed, can do the same in Smarty.
Optimal would be to write a custom modifier that would compose an entire link. Something like
Code:
  function smarty_modifier_???($source, $rel = "stylesheet", $type = "text/css")
  {
    return sprintf("<link rel=\"%s\" href=\"%s?%u\" type=\"%s\"/>\n", $rel, $source, filemtime("{$_SERVER['DOCUMENT_ROOT']}/$source"), $type);
  }


Then
Code:
{"/default.css"|smarty_modifier_???}


Do make sure that the results of your transform are cached within template. Else you'd waste FS calls on polling mtime for each file each time your template is rendered.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP