Adding a jQuery plugin to a block

Permalink
I'm working on my first add-on, trying to include a jQuery plug-in with the package. I can't get C5 to recognize & load the .js file. This is a relatively simple template to modify the search block.

The jQuery file is stored in the root folder of the package. I'm calling it with this function:

public function on_page_view() {
$html = Loader::helper('html');   $this->addHeaderItem($html->javascript(DIR_REL.'/packages/coolinput_search/jquery.coolinput.js'));
}


When the template is applied to the block, the page should load the .js... but it's not getting added to the <HEAD> of the page.

I can get the code to work by including it in my view.js file, but I'd prefer a cleaner approach.

Any thoughts on a better method/better location to get this file loaded, or am I just missing something?

Sherm Stevens
http://www.invision-studios.com

invision
 
Mnkras replied on at Permalink Reply
Mnkras
thats not how the html helper works

in /packages/package_handle/ make a /js/ folder
and put the js file in it, then
$html->javascript('filename.js', 'pkg_handle');
invision replied on at Permalink Reply
invision
Mnkras,

I was following the instructions here:
http://www.concrete5.org/documentation/how-tos/javascript-jquery-an...

I included the code in my original post in /packages/package_handle/controller.php. Does a block need another controller.php file in the block folder to use the on_page_view() function?

I appreciate your patience. Just trying to put an installable block together to share back with the community, since I've gotten so much out of C5.
jordanlev replied on at Permalink Reply
jordanlev
I could be wrong, but I think I remember running into trouble using the on_page_view() event in a package controller because it doesn't get called by the system at the right time to include it in the page (so a bug in the system).

If this is a custom template, what you can do is create a folder inside the block folder called "js", and any files in there should be automatically included by the system, without you having to call addHeaderItems(). So if this is a custom template for the search block, you'd create this folder:
/packages/package_handle/blocks/search/templates/template_name/js/

and put the javascript files in there.
Mnkras replied on at Permalink Best Answer Reply
Mnkras
oh, sorry, i though it was for a package,

so in /packages/pkgname/blocks/blkname/

make a folder called js and any js put in there is auto included
invision replied on at Permalink Reply
invision
It all makes sense now.

jordanlev, Mnkras, thanks for your help. Sorry I can't choose both as the best answer.