I'm working on a site that makes extensive use of CSS image replacement techniques (see: mezzoblue).
I'm generating the images using a custom "text2image" module inspired by signwriter, but using imagemagick instead of gd (cause imagemagick is just better).
To speed up development - for the time being - I'm emitting <style type="text/css"/> tags straight into the HTML code. However this is not acceptable anymore once the sites goes into production. For one, it's just plain ugly coding and what's even worse, it's nowhere near standards-compliant. A <style/> tag just isn't allowed in the body of an html document.
What I would like to do is generate stylesheets on the fly and have them added to the head of the document, like drupal_add_css(), but this function takes a file path as argument, not inline CSS code, so this is not an option.
I would like a custom CSS file to be aggregated for every page in the site, or for every text2image profile, or for every menu, or for, ... Catch my drift?
An excerpt from my code:
<?php
function theme_menu_item_link($link) {
// ...code removed for brevity...
// generate and cache an image from some text
// text2image_cache_advanced($text, $profile) returns array(
// 'path' => PATH TO FILE,
// 'url' => FULL URL TO FILE,
// 'text' => THE TEXT,
// 'profile' => THE PROFILE,
// 'info' => THE IMAGE INFO (from imageapi_image_open(...)['info'])
// )
$t2i_normal = text2image_cache_advanced(strtoupper($link['title']), 'menu-item');
// ...code removed for brevity...
// generate CSS styles for the item
$t2i_css .= '#'. $id .' { display: block; background: transparent url('. $t2i_normal['url'] .') no-repeat 0 0; width: '. $t2i_normal['info']['width'] .'px; height: '. $t2i_normal['info']['height'] .'px; }'. "\n";
// ...code removed for brevity...
return l('<span>'. $link['title'] .'</span><style type="text/css">'. $t2i_css .'</style>', $link['href'], $link['localized_options']);
}
?>Obviously the last line of the code should be altered not to emit the <style type="text/css">. Instead the CSS code should be emitted into an aggregated CSS file that can be included in the head of the document.
Any suggestions on this subject would be greatly appreciated, since I'm pretty new to Drupal (and PHP in general for that matter).
Comments
Roll your own stylesheet
Could you not write out your custom css into a .css that you place in, say, the files folder? Then you can load the stylesheet with drupal_add_css() and it would be automatically aggregated and cached.
Garland does something like that. When you specify colors and what have you, it generates a custom-made stylesheet and places it in the files folder.