I created a new block for my custom module that requires a css file and a few javascript files to be added to the template via the Preloads.inc file. I used the Preloads.inc from the rating module as a template to create mine. When the page that contains the block is loaded it does not include the required .css & .js files. Is there some additional configuration I need to do so that Preloads.inc is called when my block is used?
Thanks!
Posts: 32509
- the name of the module ($module->getId()) must be the same as the folder name of the module.
- it must be the preifx of your preload class name
- pleaae post your Preloads.inc file
- what g2 version are you coding against?
- are you sure your block is included in that page?
Posts: 6
My module name is 'favorites' and it is in the modules/favorites folder. I've included the Preloads.inc code below. When debug mode is turned on I don't get any errors concerning this file and other than these includes my block shows up fine in the template that I've included it in. I'm running Gallery 2.1.1.
<?php
class FavoritesPreloads {
function preload(&$template, $preload, $paramSet) {
static $loaded;
if (!empty($loaded[$preload])) {
return null;
}
$loaded[$preload] = 1;
$template->style('modules/favorites/favorites.css');
$template->javascript('lib/javascript/XmlHttp.js');
$template->javascript('lib/javascript/prototype.js');
$template->javascript('lib/javascript/scriptaculous.js');
$template->javascript('modules/favorites/js/favorites.js');
return null;
}
}
?>
Posts: 32509
aha!
you need to use the site admin -> themes -> ... block settings UI to add a block to a theme blcok list, else the associated preloads are not called!
doing a {g->block ...} in a .tpl file doesn't trigger the preload.
why?
because we do everything in a single pass. and when we reach the output stage where we parse the .tpl files, we're already outputting and can't add stylesheets / javascript files in the <head> section.
when you add a block via the UI, we know about it already before we start outputting and thus it's not too late to add javascript files / stylesheets to the page.
Posts: 6
Makes sense. That fixed it. Thanks!
Posts: 8601
alternatively, in theme.inc you can call $this->preloadBlock($template, 'module.Block');
see floatrix/theme.inc for an example.