Skip to content

Instantly share code, notes, and snippets.

@dongilbert
Created February 7, 2013 18:01
Show Gist options
  • Select an option

  • Save dongilbert/4732837 to your computer and use it in GitHub Desktop.

Select an option

Save dongilbert/4732837 to your computer and use it in GitHub Desktop.
Build body classes for your template from common available variables.

BodyClass Generator

This method will create a string of body classes based on common variables, like the currently active option, view, and layout. It also splits the path of the currently loaded URL and puts it into the class string.

The advantages of this is that it allows you to style views as well as layouts and specific pages on your site.

Usage

<html>
	<!-- Head data -->
	<body class="<?php echo EEHelper::getBodyClasses(); ?>">
		<!-- Body data -->
	</body>
</html>
<?php defined()
abstract class EEHelper
{
/**
* Method to get body classes for template.
*
* @return string Class string for the <body> tag
*
*/
public static function getBodyClasses()
{
$app = JFactory::getApplication();
$active = $app->getMenu()->getActive();
$bodyClasses = array();
$option = $app->input->getWord('option');
$view = $app->input->getWord('view');
$layout = $app->input->getWord('layout', 'default');
$uriParts = explode('/', JUri::getInstance()->toString(array('path')));
foreach ($uriParts as $part)
{
if (!empty($part) && $part !== 'index.php')
{
array_push($bodyClasses, strtolower(str_replace(array(' ', '.'), '-', $part)));
}
}
if (!in_array($active->alias, $bodyClasses))
{
array_push($bodyClasses, $active->alias);
}
array_push($bodyClasses, str_replace('com_', '', "{$option}-{$view}-{$layout}"));
return trim(implode(' ', $bodyClasses));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment