What I want to achieve: I want a local js file included in the <head> only when a particular static block type is called.

Example: When "gallery/view" block type is called like so, {{block type="gallery/view" name="catalog.gallery" template="catalog/category/category-gallery.phtml"}}, I want gallery.js to load.

The issue: I believe the layout xml has already been processed before magento renders static blocks. So I don't think I can add to the <head> from within the Namespace_Gallery_Block_View class.

Obviously I could just add gallery.js via local.xml on every page but the entire point of this is to limit filesize and only call the resources that are absolutely needed per page.

Additionally, I want to use js combination so i have to actually update the layout somehow.

Does anyone have an alternate method or idea to get this working in this manner? I'm not sure if what I want do is actually possible.

1 Answer 1


Add this somewhere in category-gallery.phtml

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'url';    

  • Sorry, should have been more specific. I want to use magento's js combination feature so the js file has to register with the layout system in some way well before php is done executing. Commented Jul 27, 2017 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.