Using Dynamic CSS

#1
[eluser]maffo[/eluser]
Does anybody on this board use a dynamic CSS file?

What I mean is, the values of items can be variables.

Code:
div.content{
background-color: #<?=$divContentBgCol;?>;
color:   #<?=$divContentTxtCol;?>;
}

This is just a rough example, but I have implemented it into one of my sites (partially) and it works a treat.

My question really is about performance. With this system you need to save you css within a php file and not as a css file.

Is there a downside to this? Will performance lack? How about caching etc?

Just looking for other peoples opinions

#2
[eluser]Michael Wales[/eluser]
Your only real concern is to make sure you are using the header() function so the browser understands that this is a CSS file (regardless of the extension).

#3
[eluser]llbbl[/eluser]
You can also use javascript to switch between two completely different css files. Here is a good article on dynamic CSS with php.

#4
[eluser]maffo[/eluser]
Thanks for the replies guys, so you dont think there will be any perfomance difference as long as the header() function is used?

#5
[eluser]nmweb[/eluser]
There will be a (minimal) difference in performance. It has to be processed by php whereas otherwise it wouldn't. If you send the proper headers it will be cached by the browser and if you use a server cache the php overhead could be minimized. I would mostly not use php to realtime generate css. Only if there are pressing reasons to have dynamic css I would do so.

#6
[eluser]maffo[/eluser]
The reason I would like to have different CSS variables is because I would like to have different color schemes for different pages.

For example, I have a football site I want to write, (football with feet, not hands). For each team, i want to have a different color scheme for each team. Example, Man United - Red, Black And White. Chelsea - Blue Yellow and White.

Each color scheme can be stored in the database and called upon when a user goes to a different clubs page.

The system I have developed does work but Im not too sure if im doing it in the most efficient way thats all.

Take this code for example as the header of my view layout.

Code:
<html>
<head>
<title>My Site</title>

<style type="text/css"><?=$stylesheet;?></style>
</head>

Where $stylesheet is a model and sent back as css to the controller.

The issue with this is that the the css can be viewed in the source code of each page when browsed.

If I was to use the header() function, does that not have to appear before <html> ??

#7
[eluser]Michael Wales[/eluser]
Quote:If I was to use the header() function, does that not have to appear before <html> ??

Yes it has to appear before any form of output (including whitespace).

#8
[eluser]llbbl[/eluser]
If you have php page as your css it will still get rendered so any variables won't showup unless they are set to a value.

#9
[eluser]Phil Sturgeon[/eluser]
This is a problem I was contemplating the other day. I use a modular asset managing helper (functions that calls pics, css, etc and allow for awesome organisation) and I came accross the problem that while I have managed to completly solve my "how to point to images" within PHP files, I cannot do the same in CSS.

So, if i have <?=image_asset('not_porn.jpg');?> in my PHP files, whats the best way to go about a similar thing in CSS? I was considering some CRAZY solution for using a CSS controller for making partial css views, but thats just retarded. Anyone else have a better plan?

#10
[eluser]Phil Sturgeon[/eluser]
I could always use vanilla PHP and just include the helper files directly into a PHPed CSS file, again though... dirty and BAD!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.