Use PHP to supply Javascript file name

Permalink
I am trying to set up auto versioning for Javascript files. I need to put the following in the header:
|script type="text/javascript" src="|?php echo(autoVersion(array(path=>'/js/', file=>'thescript.js'))) ?>">|/script>
and put the autoVersion function if the most appropriate place.

Currently I putting the |script type="text/javascript" src="/js/thescript.js">|/script> in the pages properties 'Header Extra Content'.

Ideally I'd like to continue to use the page properties but I can't work out how to do it.

I am new to C5, can anyone save me some time and advsie me on the best way to do this?

 
JohntheFish replied on at Permalink Reply
JohntheFish
In C5 Javascript files are in many cases automatically loaded. Where you need to specify them directly C5's addHeaderItem() makes sure they are only included once.

Either way, C5 takes care of script file versioning. If you use the browser developer console to investigate the files loaded, you will see they all have a versioning parameter.
for example,
http://www.concrete5.org/concrete/js/jquery.js?v=3e0818b9a0118a731eba69712a0bc3e5
WhitsundaysGreg replied on at Permalink Reply
Thanks for the info, I'm not sure if that's what I want. A few points/queries:
1. It is unclear how the different browsers handle versioning as a GET parameter, lots of debate on this, some say according to the HTTP spec (which version??) the browsers should be ignoring the parameters as far as caching.

2. How is C5 determining the file version? I trust it doesn't generate a new 'v' parameter for each call, that's the last thing we want, we want to take advantage of the browser cache as much as possible.

In our system the files timestamp is included in the file name for the HTTP call and then this is stripped out with a REWRITE to come up with the real file name. This way we can set the cache expiry indefinitely as the browser will think the new version is a different file.
JohntheFish replied on at Permalink Reply
JohntheFish
w.r.t (2) - I could be completely wrong on this, but from the surface it looks like an MD5. So is constant unless the file content changes (you can do some code reading to be sure).
WhitsundaysGreg replied on at Permalink Reply
You could be correct, it certainly looks like an MD5. Do you know how to use this feature for for user js files that are specificed in the header extra content?
JohntheFish replied on at Permalink Reply
JohntheFish
The best way is usually to use addHeaderItem in association with the html helper. The advantage of this is that you tell C5 what you have added and it will sort out any duplicate requests.
$this->addHeaderItem($html->javascript("jquery.js"));
$this->addHeaderItem($html->css("jquery.ui.css"));
$this->addHeaderItem($html->javascript("jquery.ui.js"));


You will also often see:
<script type="text/javascript" src="<?php echo $this->getThemePath()?>/js/scriptname.js"></script>

Which works, but does not take care of the versioning or duplicates, but is OK if you are sure it is the only time any code will try and load it, like theme specific code.

You could also look at the code in the ajax lessons addon/howto. It goes through a whole bunch of alternatives and tests them to demonstrate what works where.
WhitsundaysGreg replied on at Permalink Reply
Thanks for all this, I am sure I will be able to work something out that will do what we want.

I was planning on putting the addHeaderItem in a theme specific controller but I can't seem to get working. I have built custom blocks without a problem, but can't seem to get C5 to find the 'page_type' controller.

The theme page type files are in /themes/mytheme/mypagetype.php. From all the doco that I can find and googling every keyword combination that I can think of I understand that the controller should be controllers/page_type/mypagepage.php. Is that correct?

If that is correct on what path should the controllers/.. folder be? Ideally I would like to keep it under /themes/mythem but that doesn't seem to work.

I'd really appreciate if you could point me in the correct direction.