Use Variables In CSS

First: Add this to your main index’s styles.

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

Next, create a file named style.php with the following content.

<?PHP
/*
	$foo='bar';
*/
header('content-type:text/css');
header("Expires: ".gmdate("D, d M Y H:i:s", (time()+900)) . " GMT");
$c=$_GET['c'];
if(preg_match('/\//',$c) or !preg_match('/.css/',$c))
{
	die('only local CSS files allowed!');
}
$css=load($c);
if($css=='')
{
	die('File not Found, sorry!');
}
preg_match_all("/\\$(\w+).*=.*\'(.*)\'/",$css,$constants);
for($i=0;$i<sizeof($constants[1]);$i++)
{
	$css=preg_replace('/\\$'.$constants[1][$i].'/',$constants[2][$i],$css);
}
$css=preg_replace("/\\#.*=.*?;\s+/s",'',$css);

echo $css;
/*
	Function load($file)
	reads the content of the file that you send and returns it
*/
function load($filelocation)
{
	if (file_exists($filelocation))
	{
		$newfile = fopen($filelocation,"r");
		$file_content = fread($newfile, filesize($filelocation));
		fclose($newfile);
		return $file_content;
	}
}
?>

Now in your css file, you can use variables just like this.

/*	Variables
	$fonts = '"MS Trebuchet", Arial, Sans-Serif';
	$color1 = '#999';
	$H1_size = '36px';
	$color2 = '#363';
	$color3 = '#cfc';
*/
body{
	text-align:center;
	background:#333;
	font-family:$fonts;
}
h1 {
	font-size: $H1_size;
	margin:1em auto;
	text-align:left;
	position:relative;
	width:40em;
	background:$color2;
	color: $color3;
}

admin -

Leave a Reply

You must be logged in to post a comment.