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:
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
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
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.
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.
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:
and put the javascript files in there.
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.
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
so in /packages/pkgname/blocks/blkname/
make a folder called js and any js put in there is auto included
It all makes sense now.
jordanlev, Mnkras, thanks for your help. Sorry I can't choose both as the best answer.
jordanlev, Mnkras, thanks for your help. Sorry I can't choose both as the best answer.
in /packages/package_handle/ make a /js/ folder
and put the js file in it, then
$html->javascript('filename.js', 'pkg_handle');