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

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

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

Post 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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

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

Post 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.
AntonioCS
Forum Newbie
Posts: 11
Joined: Tue Jan 15, 2008 12:31 pm

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

Post 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.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

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

Post 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.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

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

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post 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);
}
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

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

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

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

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post by Eran »

I use minify, which handles the aforementioned file combining + whitespace stripping
http://code.google.com/p/minify/
nga
Forum Commoner
Posts: 46
Joined: Mon Aug 17, 2009 3:05 am

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

Post 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?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

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

Post 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 :-(
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

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

Post by josh »

http://stackoverflow.com/questions/9840 ... pt-and-css
Check out what nategood wrote about this topic here
Post Reply