Helper "html" modified to load CSS and JS from blocks and package blocks

Permalink 1 user found helpful
Howdy,

I had been trying to find a best practice for loading in javascript and css in my blocks, and ended up modifying the core "html" helper file (hah, there's best practice for ya...)

My modification can load both CSS and JS from within a custom block's css/js directories, as well as from these directories when they're in a block that resides within a package. I enjoyed figuring out what I did, and if any others find the code useful then I'm just that much happier!

I have attached the changed css && javascript methods. Also be aware that I added the code to fix the "for the javascript addHeaderItem we need to have a full href available" issue using the same technique the css method uses (and I removed those 2 comments from the css and javascript methods).

-Landson

1 Attachment

Landson
 
PatrickHeck replied on at Permalink Reply
PatrickHeck
Just a quick remark:
If you name your files view.css and view.js they will be auto-included when displaying the block.
They need to be in the same directory with the view.php. e.g. blocks/myblock/
Landson replied on at Permalink Reply
Landson
Thanks Patrick, I am aware of that actually, but ya! The reason I made my change was that although you can use auto.js for the add and edit windows, there's no auto.css... As well, I was trying to avoid having to explicitly define a constant in my blocks holding their location (to be supplied to $html->css()/->javascript()), and using the C5 global constants instead of hard coding that location resulted in more code in my controller than I felt should be needed (especially when defining a block's location when within a package).

I want to follow standards, but I don't see why what I've implemented isn't already an option/why there's no auto.css...

My only other reason for the mod is that sometimes there's more js then I would want to put into the auto.js file, or it is easier to maintain my code by splitting it out into separate files as deemed logical.

-Landson