Th current body class system adds on the following classes to the body by default (from http://api.drupal.org/api/function/template_preprocess_page/7):
- Front page identification: front / not-front
- User logged in status: loggen-in / not-logged-in
- Page type such as page-node, page-user, etc: 'page-' . arg(0) cleaned up
- If on a page displaying or editing a node: 'node-type-'. $type
- Sidebars indication: two-sidebars / no-sidebars / one-sidebar sidebar-left / one-sidebar sidebar-right (looks for blocks in regions 'left' and 'right')
Compare this to the template suggestions system, which walks through the whole path and suggests more sensible file names. Excerpt from the code docs inside this same function.
// [...] For example,
// http://www.example.com/node/1/edit would result in the following
// suggestions:
//
// page-node-edit.tpl.php
// page-node-1.tpl.php
// page-node.tpl.php
// page.tpl.php
Bringing this same flexibility over to the body classes would be highly useful, and would allow for things like styling blocks differently on the node editing pages (body.node-edit) or styling the user profile differently for the blog author/admin (body.user-1) or styling certain regions in special ways on an Ubercart checkout page (body.cart-checkout). This system goes beyond the first argument in the path (such as node, user and cart in these cases), thus allowing for more versatile theming. Before this patch, these pages only get page-node, page-user and page-cart classes. After my patch, checking for the checkout, user 1 or node edit screen becomes trivial from CSS.
After the patch, the same code comment documenting the behavior becomes:
// [...] For
// example, http://www.example.com/node/1/edit would result in the following
// suggestions and body classes:
//
// page-node-edit.tpl.php page-node-edit
// page-node-1.tpl.php page-node-1
// page-node.tpl.php page-node
// page.tpl.php
Let's improve our toolset for themers in CSS! They deserve the same flexibility as with the theme file suggestions :)
| Comment | File | Size | Author |
|---|---|---|---|
| body-classes-based-on-suggestions.patch | 4.04 KB | gábor hojtsy |
Comments
Comment #1
johnalbinZen does this. D6 core's page-arg0 class is completely useless because it usually results in a class called "page-node". And what good is that?
I'll add "review this" to my to-do list. Shouldn't take too long to do, but no time this sec. :-)
Comment #2
johnalbinComment #3
dries commentedThis makes a lot of sense to me. It improves consistency considerably and seems to give themers more power. I'm marking this RTBC and will proceed committing this unless someone has strong objections.
Comment #4
johnalbinOk. I just reviewed this patch. This almost exactly the way Zen does it but more efficient because you are leveraging the suggestion code in
template_preprocess_page()Nice!RTBC+1 then.
Comment #5
dries commentedAlright, thanks for verifying. I committed this to CVS.