MyBB Community Forums

Full Version: Basic CSS Variables in PHP
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This offers a shorthand way to write CSS. For instance, instead of writing this:

.class
{
background: url('images/imagesubdir/image.png');
other css
}

.anotherclass
{
background: url('images/imagesubdir/image.png');
more css
}

You could write this:

.class
{
background: {BG};
other css
}

.anotherclass
{
background: {BG};
more css
}

It's incredibly easy to do and only requires a few lines of code.

Open up admin/modules/style/themes.php.

Find (around line 1900):
		// Now we have the new stylesheet, save it
		$updated_stylesheet = array(
			"cachefile" => $db->escape_string($stylesheet['name']),
			"stylesheet" => $db->escape_string(unfix_css_urls($mybb->input['stylesheet'])),
			"lastmodified" => TIME_NOW
		);

Add before:
		$replace = array(
			"{BG}"	=>	"black",
			"{TH}"	=>	"blue");

		$mybb->input['stylesheet'] = strtr($mybb->input['stylesheet'], $replace);

To add more variables, add new array entries. I just used my two test variables. These only work on the Advanced Editor and will be replaced when you save it.

If you want it to apply to new stylesheets:

Find (around line 2260):
			// Add Stylesheet			
			$insert_array = array(
				'name' => $db->escape_string($mybb->input['name']),
				'tid' => intval($mybb->input['tid']),
				'attachedto' => implode('|', array_map(array($db, "escape_string"), $attached)),
				'stylesheet' => $db->escape_string($stylesheet),
				'cachefile' => $db->escape_string(str_replace('/', '', $mybb->input['name'])),
				'lastmodified' => TIME_NOW
			);

Add before:
			$replace = array(
				"{BG}"	=>	"black",
				"{TH}"	=>	"blue");

			$stylesheet = strtr($stylesheet, $replace);

I tried making this into a plugin but always broke my Theme Editor. Toungue I hope this helps you. Smile
This can be detrimental if not used properly plus some themes use a lot of colours so declaring the variables is not an easy task especially for PHP newcomers.
This is nice, but something like LESS is probably more efficient. I'd use this if there were a way to customize/add variables and it was a plugin, though.
I actually made a guide ages back to implement LESS support but once again MyBB's resync_stylesheet() function caused all sorts of issues...