Preloads.inc not being called w/block

darrinmc

Joined: 2006-10-17
Posts: 6
Posted: Tue, 2006-10-17 04:33

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!

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Tue, 2006-10-17 08:10

- 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?

 
darrinmc

Joined: 2006-10-17
Posts: 6
Posted: Wed, 2006-10-18 00:24

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;
}
}
?>

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Wed, 2006-10-18 00:34
Quote:
nd other than these includes my block shows up fine in the template that I've included it in

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.

 
darrinmc

Joined: 2006-10-17
Posts: 6
Posted: Wed, 2006-10-18 00:46

Makes sense. That fixed it. Thanks!

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Sat, 2006-10-21 00:08

alternatively, in theme.inc you can call $this->preloadBlock($template, 'module.Block');
see floatrix/theme.inc for an example.