Is there any way to add a PHP variable, created with _phptemplate_variables(), to a CSS file (for instance common.css.php)? I have searched the forums and documentation, and I can't seem to find any information on this.

Comments

ravinggenius’s picture

If there was a way to get the current theme's name, I think I could use that instead.

ramoen’s picture

Do you mean it like this, don't think it's gonna work, anyway you have to give the css file a php extension for this to work.

CSS:


#Page{ font-size: /<?php $fsize; ?> ;
              line-height: <?php $pagelineheight; ?>;
                    }


I don't think it's a good idea to use php vars as css values...

ravinggenius’s picture

That's not exactly what I need to happen. I am using the following functions in (template.php). I am adapting these from a template I am converting.

$themedir = path_to_theme();
define('VL_WALLPAPER_DIR', $themedir.'/_wallpapers/');
define('VL_THUMBNAIL_DIR', VL_WALLPAPER_DIR.'thumbs/');
function buildWallpaperArray() {
	$d = VL_WALLPAPER_DIR;
	$wallpapers = array();
	$dir = opendir($d);
	while ($f = readdir($dir)) {
		$matches = null;
		if (eregi("(.*)\.jpg", $f, $matches)) {
			$wallpapers[$matches[1]] = VL_WALLPAPER_DIR.$f;
		}
	}
	return $wallpapers;
}
function buildThumbnailArray($wallpapers)
{
	$d = VL_THUMBNAIL_DIR;
	$thumbnails = $wallpapers;
	$dir = opendir($d);
	while ($f = readdir($dir)) {
		$matches = null;
		if (eregi("(.*)\.jpg", $f, $matches)) {
			if (array_key_exists($matches[1],$wallpapers)) {
				$thumbnails[$matches[1]] = VL_THUMBNAIL_DIR.$f;
			}
		}
	}
	return $thumbnails;
}
function spitOutWallpaperCSS($vl_wallpapers, $vl_thumbnails)
{
	$css = '';
	$countwallpapers = 0;
	foreach ($vl_wallpapers as $name => $wallpaper) {
		$css .= ".wallpaper{$countwallpapers} {\n\tbackground-image: url('{$wallpaper}');\n}\n";
		$countwallpapers++;
	}
	$countwallpapers = 0;
	foreach ($vl_thumbnails as $name => $thumbnail) {
		$css .= "#wallpaper{$countwallpapers} {\n\tbackground-image: url('{$thumbnail}');\n}\n";
		$countwallpapers++;
	}
	return $css;
}
function spitOutWallpaperThumbs($vl_wallpapers)
{
	$html = '';
	$countwallpapers = 0;
	foreach ($vl_wallpapers as $wallpaper) {
		$url = '';
		if ($_SERVER['QUERY_STRING'] === "") {
			$url .= '?';
		} else {
			$url .= '&';
		}
		$url .= "vl_wallpaper=$countwallpapers";
		$html .= "<a href=\"$url\" onclick=\"changebackground(this, {$countwallpapers}); return false;\" id=\"wallpaper{$countwallpapers}\" class=\"wallpaper\"></a>\n";
		$countwallpapers++;
	}
	return $html;
}
function _phptemplate_variables($hook, $vars)
{
	$vl_wallpapers = buildWallpaperArray();
	$vl_thumbnails = buildThumbnailArray($vl_wallpapers);
	$vars['vl_wallpaper_css']  = spitOutWallpaperCSS($vl_wallpapers, $vl_thumbnails);
	$vars['vl_wallpaper_html'] = spitOutWallpaperThumbs($vl_wallpapers);
	return $vars;
}

Essentially these functions create the HTML necessary to have little thumbnails that instantly change the the website background when clicked (with JavaScript). The beauty of this is they grab the backgrounds and optional thumbnails from a special folder inside the main theme. The thumbnails and the main backgound all depend on the CSS background property.

My page.tpl.php has the following line to place the thumbnails:
echo $vl_wallpaper_html;
which might look like:

<a href="?vl_wallpaper=0" onclick="changebackground(this, 0); return false;" id="wallpaper0" class="wallpaper"></a>
<a href="?vl_wallpaper=1" onclick="changebackground(this, 1); return false;" id="wallpaper1" class="wallpaper"></a>
<a href="?vl_wallpaper=2" onclick="changebackground(this, 2); return false;" id="wallpaper2" class="wallpaper"></a>
<a href="?vl_wallpaper=3" onclick="changebackground(this, 3); return false;" id="wallpaper3" class="wallpaper"></a>
...

And my common.css.php ends with this little snippet:

/* Begin wallpaper */
<?php echo $vl_wallpaper_css."\n"; ?>
/* END */

which is supposed to look similar to:

/* Begin wallpaper */
.wallpaper0 {
	background-image: url('sites/www.example.com/themes/vistered-little/_wallpapers/01.jpg');
}
.wallpaper1 {
	background-image: url('sites/www.example.com/themes/vistered-little/_wallpapers/02.jpg');
}
.wallpaper2 {
	background-image: url('sites/www.example.com/themes/vistered-little/_wallpapers/im-wallpaper1.jpg');
}
.wallpaper3 {
	background-image: url('sites/www.example.com/themes/vistered-little/_wallpapers/im-wallpaper6.jpg');
}
#wallpaper0 {
	background-image: url('sites/www.example.com/themes/vistered-little/_wallpapers/01.jpg');
}
#wallpaper1 {
	background-image: url('sites/www.example.com/themes/vistered-little/_wallpapers/02.jpg');
}
#wallpaper2 {
	background-image: url('sites/www.example.com/themes/vistered-little/_wallpapers/thumbs/im-wallpaper1.jpg');
}
#wallpaper3 {
	background-image: url('sites/www.example.com/themes/vistered-little/_wallpapers/thumbs/im-wallpaper6.jpg');
}
/* END */

but ends up just being

/* Begin wallpaper */

/* END */

because I cannot use the template.php-defined variable in a CSS file.

I can place both variables ($vl_wallpaper_css and $vl_wallpaper_html) in page.tpl.php, so I know they both work properly. The only problem I am faced with is adding the CSS to common.css.php instead of placing it in the HTML surrounded by

tags (which works).
platypus media’s picture

This is a real hack and slash way to do it but....

<?php
echo "<style type=\"text/css\">";
echo "#".$idName."{whatever css stuff}";
echo ".".$className."{whatever css stuff}";
echo "</style>";
?>

or something like that. It's probably not a GOOD idea to do, but it'll do the job.

Mike

ravinggenius’s picture

Thanks for your help, but that's actually not quite what I have in mind. Please see my last post for more information.