1

I'm using a custom module in my Magento installation, which uses a layout XML file to add some Javascript files into the head section of every page.

This works fine, however these javascript files need to be added AFTER the ones I've already declared in page.xml, however it would seem the XML files are processed alphabetically as opposed to hierarchically!

I've looked through the page/html_head block and there seems to be no native way to set/change the order in which items are included. Without removing the javascript files from my module's XML file and placing them in page.xml, does anyone have any idea how I could ensure the page.xml javascript is added first?

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
HughieW
  • 139
  • 1
  • 4
  • 15
  • If you have own custom module then you can override this block. As example, add optional param "sort". Next add additional block's variable with list of sort params and eventually override methods `addItem` and `getCssJsHtml`. – vikcherniy Jul 26 '12 at 11:19
  • @diNord - this does work, but there's an easier, layout-only way. – benmarks Jul 26 '12 at 12:01

1 Answers1

5

Layout XML files are processed alphabetically only as an effect of module load order, which is subject to the alphabetical ordering of glob().

While the missing sort functionality for the head block is unfortunate, there is a workaround. You can make your module dependent on Mage_Page in your module activation file.

Other options are: add a core/template block to the head block and use a template with theme-safe links to your JS files or add a core/text block and call setText() with explicit links to your JS files (less secure, generally used for CDN-hosted scripts). There is an empty getChildHtml() call in page/html/head.phtml which will render any child blocks of head.

benmarks
  • 23,384
  • 1
  • 62
  • 84
  • I think this is what I'll do - add a core/template block to the head and include the links to the JS files in question. The JS files are installed in the /js folder (not the skin) so this should be fine. I don't plan on releasing the module anyway, it's a bespoke one for this site in particular :) – HughieW Jul 26 '12 at 14:03
  • Note that if the client is using either native JS request merging or Fooman_Speedster's merge & minification, these files will not be merged if you use the template technique. – benmarks Jul 26 '12 at 16:33