We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

New plugin: ClassEE Body

Development and Programming

Derek Hogue's avatar
Derek Hogue
316 posts
16 years ago
Derek Hogue's avatar Derek Hogue

Hey all - I put this plugin together for a site I’m working on, and figured I should offer it up for others.

ClassEE Body will apply several dynamic classes to your <body> tag. Use it like so in your template:

<body{exp:classee_body}>

That’s it. You’ll now get a classed-up <body> tag using URI segments, the current member group, type of archive page (category, paged, or monthly), current browser, and platform.

For example, if the current URI was:

http://mydomain.com/magazine/articles/c/politics/P20/

Your <body> tag would look like this:

<body class="magazine articles politics category paged P20 superadmin mac safari">

(In this case, you’d be logged-in as a SuperAdmin, your category keyword would be “c”, and you’d be browsing on a Mac using Safari.)

Member groups 1 through 5 will be classed using their group names (superadmin, banned, guest, pending, member), whereas custom member groups will be classed “groupid_N” (N being the member group ID).

Numeric URI segments (for example, when calling an entry via its entry_id) will be prepended with the letter “n”, i.e.

http://mydomain.com/magazine/articles/246

Would yield:

<body class="magazine articles n246 groupid7 firefox win">

Lastly, if there are no URI segments to be found, your <body> will get the class of “home”.

If you’d like to retreive only the class names, but not the class=”” attribute itelf, simply add attr=”false” as a parameter:

{exp:classee_body attr="false"}

Download ClassEE Body from GitHub.

       
Leevi Graham's avatar
Leevi Graham
1,143 posts
16 years ago
Leevi Graham's avatar Leevi Graham

Nice work D-Rock,

I built something similar (although a little less advanced) a while ago called LG Template Info.

I’ll be using this for sure.

Quick question tho. How many extra db calls are used to determine the body class attributes?

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen
Quick question tho. How many extra db calls are used to determine the body class attributes?

I don’t think there are any database calls at all in this plugin are there?

       
Derek Hogue's avatar
Derek Hogue
316 posts
16 years ago
Derek Hogue's avatar Derek Hogue

Thanks Leevi. Mark is correct - ClassEE Body gleans info from global variables which EE populates by default, so no extra DB calls are necessary.

       
Leevi Graham's avatar
Leevi Graham
1,143 posts
16 years ago
Leevi Graham's avatar Leevi Graham

Cool, I probably should have looked at the code first.

I did notice there is no pages support which would be cool. If you throw the plugin up on github I’d be happy to fork it and contribute that functionality (or you could do it and save me time 😊 ).

       
Derek Hogue's avatar
Derek Hogue
316 posts
16 years ago
Derek Hogue's avatar Derek Hogue

Hey Leevi - I should have it on GitHub actually, not sure why I didn’t think of that. I’ll get on that.

Re: “pages support” - how do you mean exactly? Do you mean a class called “page” when viewing an EE page? All URL segments get added as classes, so if you’re on a page at /about/contact/, for example, both ‘about’ and ‘contact’ will be added as classes.

Can you clarify? 😊

       
Leevi Graham's avatar
Leevi Graham
1,143 posts
16 years ago
Leevi Graham's avatar Leevi Graham

I was thinking more along the lines of parsing the pages url to pull out the template group / template.

Say the url /contact/ rendered pages/contact as the template group / template then the following classes would be added: “tg-pages” “t-contact”

       
Derek Hogue's avatar
Derek Hogue
316 posts
16 years ago
Derek Hogue's avatar Derek Hogue

Aaah, gotcha. That’s a little beyond the scope of what I was hoping to do with the plugin right now - but maybe when I’m not so busy. I took a peek at your aforementioned plugin, and it seems like we could merge the two without too much effort. It does seem a shame to have to add DB calls to add the additional template classes though.

I did put ClassEE Body up on GitHub, after fighting with the Terminal and commit messages I couldn’t seem to save or delete or do anything with other than Command-Q-ing. (This is why I bought Versions for subversion needs!)

http://github.com/amphibian/pi.classee_body.ee_addon/

If you’re up for it, fork-away. 😊

       
Zac G.'s avatar
Zac G.
268 posts
16 years ago
Zac G.'s avatar Zac G.

Great plugin, Thanks! I have been unconsciously wanting something like this. Reminds me of the old days working with sandbox for WP ;p

       
Derek Hogue's avatar
Derek Hogue
316 posts
16 years ago
Derek Hogue's avatar Derek Hogue

Actually, WordPress 2.7’s body_class() function was mostly the inspiration for this, which I’m pretty sure was an idea that came right from the Sandbox theme.

       
RJB's avatar
RJB
35 posts
16 years ago
RJB's avatar RJB
ClassEE Body will apply several dynamic classes to your <body> tag. Use it like so in your template:
<body{exp:classee_body}>
That’s it. You’ll now get a classed-up <body> tag using URI segments, the current member group, and type of archive page (category, paged, or monthly).

Nice work!

It would be desirable if the plugin would not output the opening and closing tags (e.g. class=” “) and instead just output the actual data — exactly like the URL Segment Variables do.

There are many positives to this approach and perhaps it could be supported in a future release or by a parameter?

       
Derek Hogue's avatar
Derek Hogue
316 posts
16 years ago
Derek Hogue's avatar Derek Hogue

Excellent idea Ryan - I’ll add a parameter for that and update the plugin ASAP.

       
Derek Hogue's avatar
Derek Hogue
316 posts
16 years ago
Derek Hogue's avatar Derek Hogue

OK, I just updated ClassEE Body to 1.0.1, which adds an optional parameter:

attr="false"

This allows you to return only the class names (but not the actual class attribute).

       
Derek Hogue's avatar
Derek Hogue
316 posts
15 years ago
Derek Hogue's avatar Derek Hogue

Small note - just updated ClassEE Body to 1.0.3 to add both browser and platform classes (i.e. ‘safari mac’, ‘ie win’, etc).

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.