I would like to setup some database values that can be applied to the styles. I would like to do something like that in style.css. Can I make Drupal to process a style.php file?

body{
    background-color:#<?php print $background_color; ?>;
}

.homeblocks  {
    margin:0;
    width:200px;
    height:100px;
    background:url(images/<?php print $block_color; ?>/block.gif) no-repeat;
}

Comments

cdale’s picture

You could checkout Awesomengine.

http://drupal.org/project/awesomengine

nevets’s picture

Yes, but it is expensive. For one the css is no longer cached by the browser so it gets reloaded on each page view. It also takes some time to generate the css this way.

A couple of alternatives are

1) Use fixed class names in style.css and apply the approriate css class to the elements (in this case body and .homeblocks). So you would have rules in style.css like

body bright-color {
  background-color: red;
}
body summer {
  background-color: green;
}

2) Another approach is to us inline styles in page.tpl.php (or other template file), for example

<body style="background-color:#<?php print $background_color; ?>;">
LiquidWeb’s picture

first of all you need create a page like style.css.php and define all that dynamic stuff in it and tell drupal to include that file ass css by adding this code to your theme info file

    stylesheets[all][] = style.css.php

If your css files are not so "dynamic" I mean if colours defined in theme settings (check theme settings api module) and does not need to be changed untill you update theme settings everything will be fine. Theme minimizing options will work fine but if you need more dynamic thing say color will change depending on time instead of adding CSS files at info file try adding them at page.tpl.php file

petersim’s picture

You cannot embed php code in a css file. You could do what you want by embedding the style information in the header in your page.tpl.php file. You could also insert and inline style in the affected element. eg.

<body  style="background-color:#<?php print $background_color; ?>">;
arnoldc’s picture

Thanks to all the responses.

After exploring further, I realize none of these approaches would work for me since I also have to deal with WYSIWYG editing.

To simplify matter, I need to keep all CSS intact as the editor is linking to the same CSS file. Instead, I create a set of folders (each contains the same set of images but with different colors). The admin is responsible to copy the desired color folder to the current color folder. Not user friendly but will get the job done for now. In the further, I probably have to write a module to automate the copying process whenever a new color theme is picked.

body{
    background:url(/files/current-style/bg_dot.gif) repeat;
}

.homeblocks  {
    margin:0;
    width:200px;
    height:100px;
    background:url(/files/current-style/block.gif) no-repeat;
}
nevets’s picture

From this comment I suspect you want to check out Integrating color module

arnoldc’s picture

Yes the core color module seems pretty neat. I am not a graphic designer and I don't want to over-haul the current templates. Thanks anyway.

Jeff Burnz’s picture

http://drupal.org/project/color_soc08 will do what you want precisely if I understand you correctly. Right now its in heavy dev but not so long ago I did pretty much what you are doing with an older version.

Its light years on from core color module and allows you to use color "tags" (as many as you want), which gives you dynamic color replacement.

GoVegan’s picture

Add these two lines to your root .htaccess file:

AddHandler application/x-httpd-php .css
php_value default_mimetype "text/css"

You don't have to add .php to the end of your css file, because this tells php to preprocess your css.

Then you can add php to your CSS file in your theme like this:

<?php
$red = "#f00";
$image_path = "/mysite/sites/all/themes/mytheme";
?>

body {
    background: <?=$red?> url('<?=$image_path?>/images/bg.jpg') 0px 0px repeat-x;
}