Posted by scafmac on April 19, 2010 at 5:40pm
Using drupal_add_js($data, 'setting') doesn't appear to work from the theme. I've tried adding a setting from the template.php as well as the page.tpl file - neither worked. If I move the same exact code to a module, it worked fine.
I've combed through the D6 api page for drupal_add_js & saw no mention that the setting type wasn't an option from a theme. Am I missing something? Is there any way to pass a JS setting from a theme?
Comments
It might not be possible, but
It might not be possible, but a way around it would be to write a JS file which manipulates the Drupal.settings object directly, then include that script in your theme's .info file. Perhaps not ideal, but I don't think adding settings from the theme layer is ideal anyway, so there you go.
The Boise Drupal Guy!
Why is it a bad idea?
I'm not really looking for a hack, per say. I'm confident I could shoehorn it in there somewhere, but I'm curious about your assessment that it's a bad idea to begin with. Care to elaborate?
Here's some context. We have a few dozen Drupal sites at work. We generally organize them as multi sites so there are fewer versions of core to support. We have about 6 different customized themes we've developed, all based on Zen. We have a standard installation profile that configures supported modules & builds some custom CTs that we've developed.
The key bit is that multiple sites will be sharing any theme or module, unless it is only available to a single sub-site, which is the exception with themes. So no theme can have any site specific details hardcoded.
All I'm trying to do is pass the site name into the drupal.settings object. That's used in some enhancements in the themes. It can't be hardcoded in the theme, since the themes is used by multiple sites. It seems silly to move the JS enhancements out of the theme, when they are clearly theme specific.
So what I've tried is calling drupal_add_js in the template.php file. I've checked the returned value and it looks like the setting has been added properly, when the template file is run. It just seems that it must get processed a second time after the theming is finished, loosing that setting.
Do you understand why it's not getting processed?
Thanks.
I'm curious about your
Well, it just crosses a sort of psychological barrier; modules are for data, and themes are for design. When you're altering the JS settings in the theme layer, you run against that.
"lose" = opposite of "win" or "gain." "loose" = opposite of "tight." NEVER MAKE THAT MISTAKE AGAIN KTHX
I'm not entirely sure, but if I had to guess, I would say that when the template.php files and other template-related files are executed, the variables which contain the JS data to be added to the page have already been passed to the theme layer. Calling drupal_add_js() is still altering those variables, but not the copies which are passed to the theme layer. Just a guess, though.
The Boise Drupal Guy!