addHeaderItem in Block
Permalink
I red a lot discussions about that topic in this forum, but no solution worked for me. The only thing i want to do is to add a js file to the header of my page.
I have added the following function to my block controller (its a block controller that overwrites the search block controller):
The on_start function gets called but the file is not added to the header. If i do the following instead:
It works, but the code is added somewhere in the code (besides the block code). Adding to the header seems just to fail, whatever i do. I tried putting the js file in these folders: /js /themes/myTheme/js and besides the controller in /blocks/search/.
If i alter the block controller addHeaderItem function to echo the $file var it also echos the right code. its just not added to the header. I'm doing it in the on_start function, i tried on_before_render also... nothing.
Whats wrong?
I have added the following function to my block controller (its a block controller that overwrites the search block controller):
function on_start() { $html = Loader::helper('html'); $this->addHeaderItem($html->javascript('searchCustom.js')); }
The on_start function gets called but the file is not added to the header. If i do the following instead:
echo $html->javascript('searchCustom.js');
It works, but the code is added somewhere in the code (besides the block code). Adding to the header seems just to fail, whatever i do. I tried putting the js file in these folders: /js /themes/myTheme/js and besides the controller in /blocks/search/.
If i alter the block controller addHeaderItem function to echo the $file var it also echos the right code. its just not added to the header. I'm doing it in the on_start function, i tried on_before_render also... nothing.
Whats wrong?
instead of doing a function on_start have you just tried putting it in php tags?(put at very top of page)
In blocks, just make a folder named js and/or css and the contents will be automatically added to the header
That does only apply for blocks added via the C5 interface not for hard coded blocks.
If you're hardcoding a block in your page type templates, then why don't you just put a link to the javascript file in your theme's <head>, like you would with any other javascript or css file?
Finally i did but i wanted to learn why it is not working. Now i learned it is to late for the block in the overall process. Thanks for your answers.