Style Sheets
  Home arrow Style Sheets arrow Page 7 - CSS Constants
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
STYLE SHEETS

CSS Constants
By: Chris Heilmann
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2005-05-09

    Table of Contents:
  • CSS Constants
  • The CSS standard compliant approach
  • Using ID Selectors and Descendent Selectors
  • Moving server side
  • Using Server Side Scripting Languages
  • More examples
  • Parsing CSS with PHP

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    CSS Constants - Parsing CSS with PHP


    (Page 7 of 7 )

    Parsing a CSS file before it gets sent to the server allows us to come up with a notation for the constants. As an example we will use $constant = 'value'; as the definition and $constant as the implementation.

    Our CSS looks something like this:

    /* Demo CSS */
    /* Constants 
     
    $colour1 = '#999';
    $colour2 = '#363';
    $colour4 = '#696';
    $colour3 = '#cfc';
     
    */
    body{
            text-align:center;
            background:$colour2;
            font-family:"MS Trebuchet", Arial, Sans-Serif;
    }
    #boundary{
            font-size:90%;
            margin:1em auto;
            text-align:left;
            position:relative;
            width:40em;
            background:$colour2;
    }
    […]
     

    We apply it to the HTML document via the LINK or the style tag, like any other CSS; the only difference is that we send it to a PHP script for parsing:

     
    <style type="text/css">
            @import 'cssconst.php?c=demo.css';
    </style>
     

    The script to parse the CSS and write out the values of the defined constants is rather simple:

     
    <?PHP
    header('content-type:text/css');
    header("Expires: ".gmdate("D, d M Y H:i:s", 
    (time()+900)) . " GMT");
     
    // grab the c parameter and ensure that it contains 
    .css is no slashes
    // this is a safety measure to prevent XSS
    $c=$_GET['c'];
    if(preg_match('/\//',$c) or !preg_match('/.css/',$c))
    {
            die('only local CSS files allowed!');
            exit;
    }
     
    // load the content of the CSS file into the variable css,
    end if the
    // file wasn’t found.
    $css=load($c);
    if($css=='')
    {
            die('File not Found, sorry!');
            exit;
    }
    // grab all constants and store them in the array constants
    preg_match_all("/\\$(\w+).*=.*\'(.*)\'/",$css,$constants);
    for($i=0;$i<sizeof($constants[1]);$i++)
    {
     
    // replace all occurrences of the contants with their values
            $css=preg_replace('/\\$'.$constants[1][$i].'/',
    $constants[2][$i],$css);
    }
     
    // delete all constant definitions
    $css=preg_replace("/\\#.*=.*?;\s+/s",'',$css);
     
    // print out the style sheet
    echo $css;
     
    function load($filelocation)
    {
            if (file_exists($filelocation))
            {
                   $newfile = fopen($filelocation,"r");
                   $file_content = fread($newfile, 
    filesize($filelocation));
                   fclose($newfile);
                   return $file_content;
            }
    }
    ?>

    To see it in action, check the demonstration page with the unparsed
    style sheet
    .

    Disallowing the script to read other than local files and only CSS files are the basic safety measures we should enforce when using this technology. Allowing any script in any location to be parsed might enable visitors to spy on sections of the server they are not supposed to look into.

    For basic operations, this is a slick approach to the problem. However, to allow for more complex definitions, we might have to amend the regular expressions –- for example, to allow for multi-line definitions or the usage of quotation marks within a definition.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Just a suggestion.Safari seems to struggle with just:header(’Content-type:...
     

    STYLE SHEETS ARTICLES

    - Image Replacement CSS Techniques
    - Using BlueTrip`s Success, Notice and Error C...
    - More Uses for the Thin and Caps CSS Classes ...
    - Styling Definition Lists with the BlueTrip C...
    - Styling Unordered and Ordered HTML Lists wit...
    - Using the BlueTrip CSS Framework`s Thin and ...
    - Adding Borders to Web Page Columns with Blue...
    - Introducing the BlueTrip CSS Framework
    - Using a Background Grid to Assist Web Page L...
    - Extending the Rule Of Thirds for Web Page La...
    - A Two-Column Web Page Layout Based on the Ru...
    - Using the Rule Of Thirds for Web Page Layout
    - Swapping Columns Using the Divine Ratio for ...
    - Using the Golden Ratio in Liquid Web Page De...
    - Fundamental Design Principles for Web Page L...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek