Forum FAQForum FAQSearchSearch MemberlistMemberlist Forum ignore listForum ignore list RegisterRegister ProfileProfile Log in to check your private messagesLog in to check your private messages Log inLog in
"No More CSS Hacks" for the non-programmer

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    mi3dot.org Forum Index -> Client-side
View previous topic :: View next topic  
Author Message
andrej`
Guest





PostPosted: 14.01.2006 13:34    Post subject: "No More CSS Hacks" for the non-programmer Reply with quote

Quote sa stylegala foruma

Quote:
Intro

Today I read David Hellsing's "No More CSS Hacks" which (if you haven't read it) explains and gives examples to for how to go about serving up browser specific css using php... essentially replacing css hacks. It's a great article and very informative. However, it's my opinion that the sight of all that php code can scare away the non-programmers among us.

I've been using server-side css "hacks" for the half-year or so. At some point (I guess about four months ago) I got sick of writing repetitive php if statements in my css. It took to long, and it made it harder to read. So I wrote a simple markup and parser to save myself time.

What it does?

Instead of:

#container {
<?php if ($browser == "internet explorer") { ?>
width: 600px;
<?php } else { ?>
width: 400px;
<?php } ?>


You write:

#container {
<if internet explorer>
width: 600px;
<else>
width: 400px;
<end>


More examples:

<if internet explorer 6>
<if less than internet explorer 6>
<if not firefox 1.5>
<if greater than firefox 0.8>
<if opera>


It's my hope that this nice and easy syntax will make the use of server-side css hacks a little less scary for the non-programmers among us.

How to use

Rename your stylesheet to give it a ".php" extension.

Add this to beginning of stylesheet:

<?php ob_start(); ?>


Add this to end of stylesheet:

<?php
header("Content-type: text/css");
$stylesheet = ob_get_clean();
$browsers = array("OPERA", "MSIE", "NETSCAPE", "FIREFOX", "SAFARI", "KONQUEROR", "MOZILLA");
$browser = "OTHER";
$version = 0.0;
foreach ($browsers as $x) {
if (($s = strpos(strtoupper($_SERVER['HTTP_USER_AGENT']), strtoupper($x))) !== FALSE) {
$version = (double) preg_replace("/[^0-9,.]/", "", substr($_SERVER['HTTP_USER_AGENT'], $s + strlen($x), 6));
$browser = $x == "MSIE" ? "internet explorer" : strtolower($x);
break;
}
}
preg_match_all("/\<if(?P<not> not )?(?P<less> less than )?(?P<greater> greater than )? ?(?P<browser>[a-zA-Z ]*?) ?(?P<version>[0-9\.]*?)? ?\>/mi", $stylesheet, $matches);
for ($x = 0; $x < count($matches[0]); $x ++) {
$start = strpos($stylesheet, $matches[0][$x]);
$end = strpos($stylesheet, "<end>", $start);
$else = strpos($stylesheet, "<else>", $start);
$length = strlen($matches[0][$x]);
if (! ($matches["less"][$x] && $version < $matches["version"][$x] || $matches["greater"][$x] && $version > $matches["version"][$x] || ($matches["not"][$x] ? (($matches["version"][$x] && $matches["version"][$x] == $version) || ($matches["browser"][$x] == $browser)) : (($matches["version"][$x] && $matches["sversion"][$x] != $version) || ($matches["browser"][$x] != $browser))))) {
if ($else < $end && $else) {
$stylesheet = substr($stylesheet, 0, $start) . substr($stylesheet, $start + $length, $else - $start - $length) . substr($stylesheet, $end + 5);
} else {
$stylesheet = substr($stylesheet, 0, $start) . substr($stylesheet, $start + $length, $end - $start - $length) . substr($stylesheet, $end + 5);
}
} else {
if ($else < $end && $else) {
$stylesheet = substr($stylesheet, 0, $start) . substr($stylesheet, $else + 6, $end - $else - 6) . substr($stylesheet, $end + 5);
} else {
$stylesheet = substr($stylesheet, 0, $start) . substr($stylesheet, $end + 5);
}
}

}
echo(preg_replace("/\*\//", "*/\n" , preg_replace("/}/", "}\n" , preg_replace("/\n[ \t]*\r/", "" , $stylesheet))));
?>


And you're done. Begin adding special statements.

Additional Notes:

Browsers are labeled with lower case letters (examples: internet explorer, netscape, firefox, opera).

Don't try to user "greater than" or "less than" in conjunction with "not"... that's just asking for trouble.

I'm sure that that code isn't perfect. I haven't uber-extensively tested it. If you spot any errors, please let me know.

By rearranging the order of browser detection (adding opera before msie) you should be able to sniff out opera even when it pretends to be internet explorer.

I've added a few simple regular expressions at the end to make the output neat and tidy in terms of line-spacing.


Nisam probao još, ali čini mi se jako dobro i jednostavno za koristit. Mišljenja?
Back to top
niksy



Joined: 29 Sep 2003
Posts: 727

PostPosted: 14.01.2006 14:26    Post subject: Add user to your forum ignore list Reply with quote

Meni se ovo cini kao super stvar, dobijes jednostavniji pregled u kod.
Sulien, maratz? Very Happy
Back to top
View user's profile Send private message Visit poster's website Twitter profile
Sulien



Joined: 04 Jan 2004
Posts: 2905
Location: Zagreb

PostPosted: 14.01.2006 14:34    Post subject: Add user to your forum ignore list Reply with quote

Ljudi koriste PHP za CSS već jako dugo, tako i ja - kad treba. A u većini slučajeva mi ne treba.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
carr



Joined: 13 Nov 2003
Posts: 1397
Location: Karlovac

PostPosted: 14.01.2006 18:24    Post subject: Add user to your forum ignore list Reply with quote

meni se ovo ne svidja, i kvotam suliena po tome.

_________________
Bolje biti malo lud, nego malo pametan.
Back to top
View user's profile Send private message Visit poster's website
Gale



Joined: 04 Apr 2005
Posts: 120

PostPosted: 14.01.2006 18:49    Post subject: Add user to your forum ignore list Reply with quote

Stara stvar
Back to top
View user's profile Send private message Visit poster's website
/dev/null



Joined: 22 Dec 2004
Posts: 144

PostPosted: 15.01.2006 16:56    Post subject: Add user to your forum ignore list Reply with quote

A kada neko ima custom agent string? Ovo nije konacno resenjea a samim tim ja mislim da je sucks
Back to top
View user's profile Send private message
sime
Guest





PostPosted: 15.01.2006 20:01    Post subject: Reply with quote

smeće(tm)
Back to top
budha



Joined: 02 Mar 2004
Posts: 1377
Location: Osijek

PostPosted: 16.01.2006 11:53    Post subject: Add user to your forum ignore list Reply with quote

bzveze. jel toliki problem naucit PHP if-else sintaksu?

_________________
I hate to advocate drugs, alcohol, violence, or insanity to anyone, but they've always worked for me.
Back to top
View user's profile Send private message Visit poster's website
andrej`
Guest





PostPosted: 16.01.2006 12:16    Post subject: Reply with quote

budha wrote:
bzveze. jel toliki problem naucit PHP if-else sintaksu?


Neki to vole ovako Very Happy
Back to top
Samurai



Joined: 22 Dec 2005
Posts: 88
Location: Croatia

PostPosted: 16.01.2006 12:23    Post subject: Add user to your forum ignore list Reply with quote

With all do respect David Hellsing, ...let's not complicate things. Smile

_________________
mcville.net check out some fresh design /coder interview's Wink
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
budha



Joined: 02 Mar 2004
Posts: 1377
Location: Osijek

PostPosted: 16.01.2006 23:05    Post subject: Add user to your forum ignore list Reply with quote

andrej` wrote:
budha wrote:
bzveze. jel toliki problem naucit PHP if-else sintaksu?


Neki to vole ovako Very Happy

to ne iskljucuje tvrdnju da je rjesenje glupo Wink

_________________
I hate to advocate drugs, alcohol, violence, or insanity to anyone, but they've always worked for me.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    mi3dot.org Forum Index -> Client-side All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group