I come from a bit of a Drupal background, and I really like hey they add classes to your body tag based on the page you're visiting. For instance, if you're on a page with URI /page/episode/01, your body tag may look like:
<body class="html not-front not-logged-in no-sidebars page-episode page-episode- page-episode-01 episode" >
It lets you effectively style different views, content types, etc. individually all from the one stylesheet without effecting different components. I had a quick search for something in Yii, but unable to find something, quickly threw something together.
In protected/components/controller.php, add:
/** * @var string the classes that should be displayed in the body element of each page. */ public $classes; /** * Make sure we run the parent's constructor method, and call the function to calculate * what classes to use. */ public function __construct($id,$module=null) { parent::__construct($id, $module); $this->getBodyClasses(); } /** * For easier styling, let's insert some classes from the URI in to our body element */ public function getBodyClasses() { if (! empty(Yii::app()->baseUrl)) { $uri = explode(Yii::app()->baseUrl, Yii::app()->request->requestUri); unset($uri[0]); $components = explode("/", implode(Yii::app()->baseUrl, $uri)); } else { $components = explode("/", Yii::app()->request->requestUri); } foreach ($components as $id => $component) { if (empty($component)) { unset($components[$id]); } } ksort($components); $class = ''; for ($x = 1; $x <= sizeOf($components); $x++) { if ($x <> 1) { $class .= '-'; } $class .= $components[$x]; $classes[] = strip_tags($class); } if (isset($classes)) { $this->classes = implode(' ', $classes); } }
And then in your layout view just add:
<body class="<?php echo isset($this->classes) ? $this->classes : '' ?>">
And then if you were on:
example.com/forum/example/news/welcome-topic-1
Your body tag would look like:
<body class="forum forum-example forum-example-news forum-mixxi-news-welcome-mixxi-1">
Allowing you to style elements on the forum, or for that particular parent forum (and child forum), or even the topic if you wanted.
Again, if anybody can think of a more efficient way, please let me know.