Page 1 of 1

Do you use .php instead of .css for dynamic style sheets?

Posted: Mon Dec 07, 2009 9:01 am
by Apollo
CSS allows us to keep layout stuff centralized in one place. Saves lotsa work. But sometimes it would come in handy to take things a step further. CSS is not dynamic, and cannot contain dependencies or symbolic relations.

But what about not putting the CSS directly in a .css file, but output it from a .php file instead, just like we generate HTML in .php files rather than storing it directly in .html ?

This allows for all kinds of cool constructions.

For example, suppose some .html contains:

Code: Select all

<link href="DynamicStyleSheet.php" rel="stylesheet" type="text/css">
And DynamicStyleSheet.php looks like this:

Code: Select all

<?php
$size = 600; 
$sizeLeft = intval($size/3);
$sizeRight = $size - $sizeLeft;
print(".column1 { width: $sizeLeft px; }");
print(".column2 { width: $sizeRight px; }");
?>
Obviously this is just a stupid simple example, but you get the idea.

Do you people ever use .php files like this to dynamically generate CSS content?

Re: Do you use .php instead of .css for dynamic style sheets?

Posted: Mon Dec 07, 2009 10:03 am
by VladSun
I always use PHP generated CSS, though I do it mainly for performance purposes - e.g. to put several CSS files into a single one.

Re: Do you use .php instead of .css for dynamic style sheets?

Posted: Mon Dec 07, 2009 4:39 pm
by daedalus__
im sure a lot of people do things like this. i was trying to have an idea about using php to create pages in a way similar to windows forms but it got too big for my head.

Re: Do you use .php instead of .css for dynamic style sheets?

Posted: Mon Dec 07, 2009 5:09 pm
by AntonioCS
Why not just have a php script that puts them all in one big css file? If you do that, there is no need to run a php every time you run css file.

If you are worried about updating, don't be. When you are done creating your css, just call the script and it will recreate the big css.
PHP is great, but if you can just use a normal css file then use it.

Re: Do you use .php instead of .css for dynamic style sheets?

Posted: Mon Dec 07, 2009 6:06 pm
by daedalus__
it'd be really okay to use php to combine css files. i like to separate large css files into sections. import requests the file using http, which isn't really a big deal until you have a large user base.

another reason its useful to combine them using php is for compatibility with very old browsers (and what about mobile browsers) that don't support the import rule.

also some versions of internet explorer dont accept media types, either.

Re: Do you use .php instead of .css for dynamic style sheets?

Posted: Tue Dec 08, 2009 3:41 am
by Apollo
VladSun wrote:I always use PHP generated CSS, though I do it mainly for performance purposes - e.g. to put several CSS files into a single one.
How do you mean several CSS files into one?

One big stylesheet.php, and then from different html/php pages you include stylesheet.php?s=id1, stylesheet.php?s=id2, etc to get the different CSS contents? (which then may of course share lotsa CSS internally, is that where your performance gain comes from?)
AntonioCS wrote:Why not just have a php script that puts them all in one big css file? If you do that, there is no need to run a php every time you run css file.

If you are worried about updating, don't be. When you are done creating your css, just call the script and it will recreate the big css.
PHP is great, but if you can just use a normal css file then use it.
Eh, you mean a hidden/internal .php script that litterally writes a .css file on your webserver?

Can do, but then you still can't have truly dynamic CSS, e.g. depending on user settings or actual database content.

Re: Do you use .php instead of .css for dynamic style sheets?

Posted: Tue Dec 08, 2009 3:58 am
by VladSun
My css directories have ordinary CSS files in them. But instead of loading them one by one I use a simple PHP call to read a list of required CSS files (may be all of them) into a single HTTP session.

Code: Select all

if (!headers_sent())
{
    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-type: text/css; charset=utf-8');
}
 
 
$hasErrors = false;
$s = "";
foreach ($files as $file)
{
    if (!file_exists('css/'. $file .'.css'))
    {
        $s .= "CSS file ".$file." not found.\r\n";
        $hasErrors = true;
    }
}
 
if (!$hasErrors)
{
    foreach ($files as $file)
    {
        readfile('css/' . $file . '.css')."\n";
        echo "\r\n";
    }
}
else
{
    error_log($s);
    die($s);
}

Re: Do you use .php instead of .css for dynamic style sheets?

Posted: Tue Dec 08, 2009 5:09 pm
by daedalus__
the system im working on involves adding css files to a stack. then when the view is output the css is combined into a single file and all unneccasary whitespace is removed.

Re: Do you use .php instead of .css for dynamic style sheets?

Posted: Tue Dec 08, 2009 5:42 pm
by pickle
I did this once. Left a bad taste in my mouth. I prefer to build my designs their stylesheets so they're as flexible as possible. The only reason I can think of for PHP-ifying stylesheets is so you can use the same colour value (for example) multiple times. But that's what multiple classes are for.

Re: Do you use .php instead of .css for dynamic style sheets?

Posted: Wed Dec 09, 2009 9:28 am
by PHPHorizons
My take on it is, serve up .css files and leave out the php. If you need lots of different css files, then you should generate them on the original page load, cache it, and then put a link to the cached css file in your html. I'll rephrased that.

A user loads a page on your site, let's say index.php. The code in index.php checks some user settings like the desired font color, background color, etc. A css file name is computed using the options the user has stored in their account. The php code checks to see if a css file exists with this file name. If it does not, a subroutine that generates the css is executed. That subroutine would write the file and cache it. Then the php code would insert a xxxxx.css LINK tag into the page that links to this cached css file.

If you need to detect ie/firefox, and serve up css sheets to certain browsers, the index.php page can change the css link accordingly, and my caching system can still be used.

This system means that for any given page load, you only need an instance of php to execute once, instead of twice.

Hope that helps.

Re: Do you use .php instead of .css for dynamic style sheets?

Posted: Wed Dec 09, 2009 9:37 am
by Eran
I use minify, which handles the aforementioned file combining + whitespace stripping
http://code.google.com/p/minify/

Re: Do you use .php instead of .css for dynamic style sheets?

Posted: Thu Dec 10, 2009 2:22 am
by nga
i use php full the admin configuration from database to generate css thus customize the website looks. Is there any other more elegant way to do this?

Re: Do you use .php instead of .css for dynamic style sheets?

Posted: Fri Dec 11, 2009 3:12 pm
by josh
Magento does this, and if there's an error its *impossible* to debug, I would say if you do it make sure you make a way to bypass it and make it well documented. Personally for me you can go to my app and change around which lines are commented, and it'll "revert" to having 100s of neatly categorized CSS files ( which take longer to load, but are easier to debug). Sure it would be easy to automate that but it would be time spent just to make debugging harder for me

As far as making CSS dynamic I will do that if the CSS is user editable ( user can login and change their profile background color for instance). Otherwise it is just not what CSS was intended for, the creator of CSS himself said he intentionally left out "dynamic" stuff because it would ruin the simplicity.

I do a lot of javascript code generation via PHP though :-), sometimes just outputting a configuration option from PHP into a javascript variable declaration so i can look at it in javascript.

It kind of disappointed me nusphere PHPED does not officially support mixed languages (it will keep reverting you to "php" view even if you tell it to view it as "js"). He made a written statement to me they will not fix :-(

Re: Do you use .php instead of .css for dynamic style sheets?

Posted: Fri Dec 11, 2009 7:34 pm
by josh
http://stackoverflow.com/questions/9840 ... pt-and-css
Check out what nategood wrote about this topic here