include js in page header

Permalink
I'm trying to include a javascript file in order to use it from a HTML block.
http://www.concrete5.org/documentation/how-tos/developers/javascrip... describes the process, but isn't entirely clear to me.

I need to add

public function on_page_view() {
$html = Loader::helper('html');
$this->addHeaderItem($html->javascript('jsfile.js'));
}

to the block controller's on_page_view, but where is this?

I've tried to add this to .../concrete/blocks/html/view.php but to no avail. I can't find the on_page_view, and I'm thoroughly stuck.

 
pumpapa replied on at Permalink Reply
Typically RTFM, although, which FM...

Found my answer inhttp://www.concrete5.org/documentation/recorded-trainings/building-...

which explains that concrete/blocks/html/view.php is largely empty because the good stuff is in concrete/core/controllers/blocks/html.php

Problem solved.
intuitivereason replied on at Permalink Reply
intuitivereason
To answer your question of where to put this, you will just need to extend the HTML block controller. Don't ever modify anything under /concrete/ as those are the core files and will get overwritten with any update. The proper way is to always extend what you're trying to change.

So

1) Under your root /blocks/ folder, create a directory called "html".

2) Go to /concrete/blocks/html/ and copy controller.php to your new html directory created in the root blocks folder.

3) Open the controller you copied and paste in the function you have above so it looks like:
class HtmlBlockController extends Concrete5_Controller_Block_Html {
    public function on_page_view() {
        $html = Loader::helper('html');
        $this->addHeaderItem($html->javascript('jsfile.js'));
    }
}


4) Whatever you call the Javascript file, make sure the file actually exists in your root /js/ folder.

You will now see the JS file included in your header. To include it in your footer, just use the "addFooterItem" method.

Lastly, if you're just trying to include a JS file globally across every page, it's much better to skip this and just include the JS file in your header or footer.
<script src="<?= $this->getThemePath(); ?>/js/jsfile.js"></script>
pumpapa replied on at Permalink Best Answer Reply
Hang ON, this doesn't seem to work

I've followed you instructions to the letter, I think. From the C5 directory:

concrete5#> more blocks/html/controller.php 
<?php 
defined('C5_EXECUTE') or die("Access Denied.");
class HtmlBlockController extends Concrete5_Controller_Block_Html {
   public function on_page_view() {
       $html = Loader::helper('html');
       $this->addHeaderItem($html->javascript('unslider.min.js'));
   }       
}
oncrete5#> ls js
unslider.min.js

Tried this and variants, but nothing gets included.
intuitivereason replied on at Permalink Reply
intuitivereason
Since this change is in the HTML blocks controller (Your first post said HTML block) this will only include the file if you're using an HTML block on your given page (as opposed to the content block). Also make sure to flush your cache if you have it enabled.

Lastly, you're doing this under /webroot/blocks/ and not /webroot/concrete/blocks/, correct?

If this still doesn't work for you, let me know.