Using Variables in a CSS File

We've all had to do it: the client wants things a little lighter/darker/bolder/slimmer/whatever and I'm up late at night search-and-replacing RGB values or color hex values throughout my CSS and JavaScript.  Yuck!  If you're a good programmer, you at least used variables in your JavaScript for those times that you needed to do styling.  But there's nothing you can do about CSS.  Or is there?

 People have posted all over the web about ways to use PHP to generate your CSS.  In fact, it's as simple as

<link type="text/css" rel="stylesheet" media="all" href="style.php" />

So what's the big deal?  Caching.  A properly cached website will load much faster than an uncached one.  This is easy to see when using Firefox and Firebug -- just switch to the "Net" tab in Firebug and Ctrl+F5 to force a full reload of your site.  Now do a regular refresh (F5).  Everything that comes back with a "304 Not Modified" status means the server didn't have to transmit that file again, the browser used a local, cached copy instead.  Because of the dynamic nature of most pages written with PHP, the default headers tell browsers not to cache anything created with PHP.  With some tweaking, we can change that:

<?php 
  $file = $_SERVER['SCRIPT_FILENAME'];
  $time = filemtime( $file );
  $etag = md5_file( $file );
  $mod = gmdate( 'D, d M Y H:i:s', $time ) . ' GMT';

  /* Check if we can used cached file */
  if(( @strtotime( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) == $time ) ||
     ( trim( $_SERVER['HTTP_IF_NONE_MATCH'] ) == $etag )) {
    header( "HTTP/1.1 304 Not Modified");
    exit;
  }  

  /* Write CSS header info */
  header( "Content-type: text/css; charset: UTF-8" );
  header( "Cache-Control: must-revalidate" );
  header( "Last-Modified: $mod" );
  header( "ETag: $etag" );
?>

Simply add this to the top of any PHP-based CSS file and it'll deal with caching based on either the Last-Modified date or the ETag value, in this case, an md5 hash of the file contents.  Better yet, save this as a cache.inc and require_once it at the beginning of your PHP-based CSS file.

After that, you can write your CSS how you like.  I prefer using heredoc notation.  For example:

$hSpace = "15px";
$vSpace = "10px";
$color = "#123456";

echo <<<END_OF_CSS
/* CSS file starts here */
div {
  margin: $hSpace $vSpace;
  color: $color;
}
/* And so on... and so on... */
   
END_OF_CSS;
?>

An interesting side-effect of this change has been that I'm much more consistant with my use of colors and spacing.  If I want to add another color to a site, the process of adding another variable forces the question, "do I really need another color?"  Similarly with spacing, because I have standard margins and paddings defined, I'm less likely to guess at how much space to put between elements and more likely to question the need for a different margin or padding amount.

What about the .css extension?

Now, some purists want CSS files to have the extension .css.  Fine...  With a little .htaccess fiddling, we can do that.  In this case, it's best if your PHP-based CSS files are all located in a single directory so you can add an .htaccess file to that directory and not burden your server with processing every .css file on your system as a PHP script.

First check to see if you server runs PHP as FastCGI or not.  Look at the output of phpinfo() and find the "Server API" entry. If it says "FastCGI," guess what...  It's running as FastCGI (this is good).  If so, add this to your .htaccess file:

AddHandler php-cgi .css
Action php-cgi /cgi-bin/php-cgi

If not, add this:

AddHandler application/x-httpd-php .css

where /cgi-bin/php-cgi points to your FastCGI binary (you can find more info at the FastCGI FAQ).

I've only tried this on one relatively small part of my website so it hasn't been thoroughly tested in a Drupal environment yet.  I'll post updates on this page if I find any problems.

UPDATE: OK, I have found one problem with this system.  If you turn on Drupal CSS optimizations, the optimizer gets completely confused by the PHP.  Works like a charm in a non-Drupal setup or with CSS optimizations turned off. Seems like there would be a quick reg-ex fix for this.  I'll have to look into this more...

UPDATE #2: A solution to the above problem with CSS optimizations is to use drupal_add_css() to add the parsed CSS files and set the preprocess parameter to false.

Permalink

2017.7.17 monday nike air max air max 2017 yeezy boost 350 adidas yeezy adidas superstar adidas nmd adidas nmd nmd adidas adidas nmd nmd adidas adidas nmd nmd shoes adidas outlet adidas shoes adidas shoes adidas uk adidas shoes adidas uk adidas shoes adidas outlet adidas superstar adidas yeezy adidas yeezy boost air jordan retro cheap jordans air jordan uk nike air max air max 95 air max cheap nike air max nike air max outlet birkenstocks birkenstock sandals birkenstock sandals birkenstock shoes birkenstocks birkenstock sandals burberry outlet canada celine outlet celine outlet store cheap jerseys cheap jerseys from china cheap jordans cheap jordans free shipping cheap jordans cheap jordan shoes cheap jordans cheap jordans cheap jordans jordan shoes cheap mlb jerseys cheap mlb jerseys mlb jerseys wholesale oakley sunglasses discount oakley sunglasses cheap ray bans chi flat iron christian louboutin outlet christian louboutin christian louboutin outlet christian louboutin christian louboutin sale christian louboutin shoes christian louboutin shoes christian louboutin outlet christian louboutin outlet christian louboutin coach outlet coach factory outlet coach outlet coach factory outlet coach outlet coach factory outlet coach outlet coach outlet online coach outlet coach outlet online coach factory outlet coach outlet store coach factory outlet coach outlet store coach outlet coach outlet store coach outlet coach canada coach outlet coach factory outlet coach outlet coach factory outlet online coach outlet coach factory outlet coach outlet coach factory outlet coach outlet coach factory outletcoach outlet coach factory outlet coach outlet coach factory outlet coach outlet store coach outlet store online clearances coach outlet store coach outlet coach factory outlet cheap nike shoes wholesale nike shoes oakley sunglasses oakley sunglasses outlet dolce and gabbana outlet doudoune moncler ed hardy ed hardy clothing fendi handbags fitflops fitflops shoes fit flops fitflops sale clearance fitflops sale clearance fred perry fred perry fred perry polo borse gucci gucci outlet gucci outlet hermes birkin hermes handbags hermes handbags hollister clothing store hollister co james harden shoes jimmy choo kate spade handbags kate spade kate spade handbags kate spade outlet kevin durant shoes kd 9 shoes longchamp bags longchamp uk longchamp handbags longchamp handbags longchamp outlet store longchamp outlet louboutin pas cher louboutin chaussures louis vuitton handbags louis vuitton handbags outlet louis vuitton outlet louis vuitton outlet louis vuitton outlet store mbt shoes michael kors handbags michael kors uk michael kors handbags michael kors handbags michael kors outlet michael kors canada michael kors outlet michael kors canada michael kors outlet michael kors outlet online michael kors outlet michael kors outlet clearance michael kors outlet michael kors outlet clearance moncler jackets moncler jackets moncler outlet moncler outlet moncler mont blanc pens mont blanc mulberry handbags mulberry purse nike air maxnike air max 90 nike air max nike air max 90 nike free flyknit nike free run flyknit nike free run black nike free 5.0 nike roshe nike roshe run nike shoes nike shoes for men nike store nike factory store nike store oakley sunglasses oakley sunglasses cheap oakley sunglasses oakley sunglasses cheap oakley sunglasses coach outlet coach factory outlet coach outlet coach factory outlet longchamp outlet louis vuitton outlet louis vuitton outlet online pandora bracelet pandora charms pandora jewelry pandora charms pandora charms sale pandora charms pandora charms pandora uk pandora jewelry pandora outlet pandora jewelry pandora outlet polo outlet polo ralph lauren outlet online prada outlet parada handbags ralph lauren ralph lauren outlet online ralph lauren outlet ralph lauren sale clearance ralph lauren outlet ralph lauren outlet online ralph lauren outlet polo ralph lauren outlet ray ban sunglasses rayban ray ban sunglasses ray ban sunglasses discount red bottoms red bottoms shoes red bottoms red bottoms shoes rolex watches replica watches sac longchamp pas cher sac longchamp pliage sac longchamp pas chersac longchamp steph curry shoes timberland boots toms shoes toms shoes outlet toms outlet toms shoes toms shoes outlet true religion outlet true religion true religion jeans true religion outlet true religion jeans valentino vans shoes vans outlet store versace shoes louis vuitton outlet yeezy boost adidas yeezy yeezy boost adidas yeezy yeezy boost yeezy boost 350 yeezy boost 350 yeezy 350 boost levis jeans adidas nmd nmd adidas adidas outlet adidas shoes adidas shoes adidas outlet adidas shoes adidas superstar adidas superstar shoes adidas trainers yeezy boost 350 adidas yeezy nike air max 90 birkenstock sandals birkenstock shoes cheap nfl jerseys cheap nfl jerseys wholesale christian louboutin christian louboutin sale coach outlet coach factory outlet online coach handbags coach bags coach outlet coach outlet store coach outlet coach outlet store online coach outlet coach outlet online hermes outlet kate spade handbags kate spade outlet kate spade outlet kate spade outlet store longchamp handbags longchamp uk michael kors handbags michael kors uk michael kors michael kors handbags michael kors outlet michael kors handbags michael kors outlet michael kors factory outlet cheap mlb jerseys mlb jerseys cheap mont blanc mulberry bags mulberry uk mulberry outlet mulberry handbags cheap nfl jerseys nike outlet nike outlet online nike outlet nike outlet store nike shoes nike shoes outlet oakley sunglasses oakley sunglasses outlet coach factory outlet official coach factory outlet michael kors michael kors outlet online michael kors outlet michael kors outlet clearance nike outlet coach outlet coach outlet store pandora pandora jewelry pandora charms pandora charms sale clearance pandora outlet pandora jewelry outlet pandora uk pandora jewelry polo ralph lauren polo ralph lauren outlet online polo ralph lauren ralph lauren outlet polo ralph lauren outlet ralph lauren sale clearance ralph lauren uk ralph lauren sale clearance polo ralph lauren swarovski crystal swarovski crystalswarovski jewellery tods outlet tods shoes tory burch outlet ultra boost yeezy boost 350 yeezy boost yeezy boost 350 v2 adidas yeezy boost 350 seattles eahawks jerseys denver broncos jerseys new england patriots jerseys dallas cowboys jerseys houston texans jerseys golden state warriors jerseys miami heat jerseys cveland cavaliers jerseys true religion jeans true religion outlet marc jacobs handbags marc jacobs bags fitflops fitflops uk cheap ray ban sunglasses ray ban sunglasses oakley sunglasses cheap oakley sunglasses ralph lauren uk polo ralph laurenkate spade outlet kate spade handbags michael kors bags michael kors uk ralph lauren uk ralph lauren outlet louboutin louboutin uk dansko shoes dansko sandals hermes bags hermes handbags louboutin shoes air jordans jordans mbt shoes michael kors outlet michael kors outlet online fit flops fitflops clarks shoes clarks outlet salomon shoes clarks shoes clarks outlet salomon shoes salomon shoes outletfitflop sandals fitflops mbt shoes rolex replica watches rolex replica rolex replica watches rolex watches fake rolex under armour shoes cat shoes dansko outletdansko outlet store valentino shoes valentino shoes outlet giuseppe zanotti outlet giuseppe zanotti sneakers parker pens supra shoes ysl handbags birkenstock outlet birkenstock sandals columbia shoes columbia outlet online tory burch outlettory burch outlet store polo ralph lauren longchamp bags longchamp uk pandora charms pandora charms uk adidas pas cher pandora bijoux birkenstock pas cher pandora jewelry nike air max pas cher nike air max polo ralph lauren adidas superstarsac michael kors pandora soldes nike store burberry outlet ray ban sunglasses discount ray ban sunglasses ecco shoes ralph lauren uk ralph lauren outlet ugg outlet ralph lauren outlet polo ralph lauren outlet pandora charms pandora jewelryugg outlet ugg outlet store jordan retro jordan retro 11 coach outlet coach outlet online michael kors outlet michael kors handbags ugg outlet ugg boots uggs ugg outlet michael kors outlet michael kors outlet store coach factory outlet coach outlet cheap nfl jerseys nfl jerseys wholesale Monday 2017.7.17

Permalink

With a trio of new colorways set to drop at the end of March, the adidas UltraBOOST 3.0 onslaught continues with news today that a “Maroon” Fitflop Sale rendition will hit shelves later this year, most likely in September. Taking the Primeknit upper and Yeezy Boost 350 V2 draping it in a maroon and black hue, adidas matches the Three Stripes cage in the Fitflops Shoes For Women same color while the heel counter and outsole don black, sandwiching a white BOOST sole. Expected Birkenstock Sale to hit $5 billion USD in sales within the next three years, adidas continues to punish http://www.adidasyeezyboost350-v2.us.com the competition with beautiful colorways like this and the upcoming bronze BOOST sole UltraBOOST 3.0.Check out Fitflop Sandals the image above and let us know if this will be cop or drop come September.
Birkenstock Shoes />adidas has announced the release date for the much-anticipated UltraBOOST 3.0 “Triple Black.” The fan favorite Adidas Yeezy Boost 350 Turtle Dove colorway is set to drop next week on March 1. The soon-to-be-coveted pair features a tonal Yeezy Boost 350 V2 Dark Green black colorway which covers everything from the Primeknit upper and heel counter to the TPU cage Fitflop Sandals and BOOST midsole. The 3.0 version also features a the new striped detailing like the rest Mbt Sandals of the 3.0 colorways. Make sure you’re ready when the adidas UltraBOOST 3.0 “Triple Black” drops Cheap Birkenstocks on March 1 at select adidas retailers and online.If you’ve missed it, check out the exploratory Birkenstock Outlet look at adidas’s revolutionary BOOST technology here.
Originally released in the early 2000′s, the adidas Originals Fitflop Outlet> Climacool 1 has seen a resurgence in the past few months. This time around, the ventilation-focused Mbt Shoes sneaker receives a ”Tactile Green” colorway. Done in a color palette dominated by gray, navy and Fitflop black the shoe features a breathable upper comprised of mesh, TPU components on the toe, sides Mbt Shoes Clearance and forefoot for additional ventilation, in addition to an adiPRENE sole unit. Retailing at ¥17,000 JPY Birkenstock (approximately $150 USD), the “Tactile Green” adidas Originals Climacool 1 is available now at retailers like Fitflops Clearance Atmos.
As we step into 2017, we are presented with yet another new rendition of the adidas UltraBOOST — this time in a striking blue colorway. The http://www.cheapbirkenstocks-outletsale.com runner features a blue primeknit upper while the cage and plastic heel cage is also in a complementary translucent blue tone. Moving down, we find the regular white BOOST sole and a black outsole to Birkenstock Sandals round things out, as well as finishing touches that include blue laces. Make sure to head Fitflop Sale over to sites such as Sneaker Politics to purchase these for $180 USD.If you still haven’t Yeezy Boost 350 caught up with the overwhelming amount of sneakers that released in 2016, check out our roundup Mbt Sandals that features the HYPEBEAST team’s favorite picks.
There are a myriad of striking variations for adidas’s Fitflops well-beloved UltraBOOST model, but this latest one from JBF Customs is a real head-turner. Cordwainer and Mbt Shoes sneaker enthusiast Jacob Ferrato strikes again with a handsome iteration of the low-top model, which pays http://www.mbtshoesoutletinc.com homage to Missoni. The UltraBOOST’s signature Primeknit upper is replaced with vegetable-tanned leather alongside the Italian Mbt imprint’s ubiquitous multicolored chevron pattern, which is adorned on the toe cap and heel cap.The model Birkenstock Store was specially made for an unnamed client, as per Nice Kicks, but be sure to request Fitflops Sandals For Women a pair at the company’s official website.
In one of the coolest sneaker twists and “what Adidas Yeezy Boost ifs” to come to life, kicks customizer DentKicksCustom took the liberty of giving the popular adidas Yeezy Boost 350 Kids UltraBOOST an atmos-themed makeover. The atmos x Nike Air Max 1, which won most votes for Mbts a re-release on Air Max Day, was one of the hottest Swoosh drops as of late. Cheap Yeezys Playing off of the collab’s popular design of grey, white, elephant print and jade color makeup, Fitflops Sale DKC thought it would be a good idea to put that inspiration onto the famed UltraBoost Fitflops Clearance model. The outcome is as clean as we’ve imagined it to be.Take a look at the Mbt Shoes Outlet images above and let us know if this custom is something you’d want as a real-life Adidas Yeezy collab in the future.

Related TAGS: Mbt Shoe Mbt Shoes On Sale Mbt Shoes Sale

Add new comment

The content of this field is kept private and will not be shown publicly.