yesjoar Posted November 11, 2012 Share Posted November 11, 2012 Description A ProcessWire module to add some classes to your body tag. That will give you a better control of your CSS. Basic Usage Add this code into the class of your body tag: <?php echo $modules->get("BodyClass")->bodyclass() ?> So your body tag should look like this: <body class="<?php echo $modules->get("BodyClass")->bodyclass() ?>"> Custom usage <?php echo $modules->get("BodyClass")->bodyclass(array('page_id' => false, 'template_name_prefix' => 'templatename-')) ?> Default options array( 'page_id' => true, // bool (default true) return the page id 'page_id_prefix' => 'page-id-', // string (default 'page-id-') prefix for page_id 'parent_page_id' => true, // bool (default true) return the page id of of parents 'parent_page_id_prefix' => 'parent-page-id-', // string (default 'parent-page-id-') prefix for parent_page_id 'template_name' => true, // bool (default true) return the template name 'template_name_prefix' => 'template-', // string (default 'template-') prefix for template_name 'browser_info' => true // bool (default true) return some browser informations (os, browser, browserversion) ); Project Page on GitHub www.yesjoar.com 3 Link to comment Share on other sites More sharing options...
nik Posted November 12, 2012 Share Posted November 12, 2012 Good job @yesjoar! Tested it a little and found a couple of tiny bugs: Variable $browser can be uninitialized at this point leading to PHP notice "Undefined variable" (fetched a page with curl). You may want to assign some default value like you've done with the rest of the variables. There's a typo here, no $u_agent defined anywhere else (anymore, I guess). You're using a question mark ("?") for browser version if no version can be found, but that's not a valid character in CSS class name - unless escaped, but that's just confusing. Dashes (-), underscores (_), letters (a-zA-Z) and numbers (0-9) are ok (yes, it's more complicated than that to be exact). Otherwise seems to be doing what it says though. Link to comment Share on other sites More sharing options...
ryan Posted November 12, 2012 Share Posted November 12, 2012 Nice job yesjoar. I am always adding template/page classes to my body tag manually and think it's always helpful when it comes to targeting stuff with CSS, especially in specific cases. So this module seems like a good idea. I also like the thinking behind the browser identification, though should mention this would not be compatible with page caching. Many (most?) sites don't need page caching, so not an issue for most I'm guessing, but just something to mention. Link to comment Share on other sites More sharing options...
AnotherAndrew Posted March 1, 2013 Share Posted March 1, 2013 Thanks for this great module. It has been really helpful! Is there a way to set a unique body class on parent pages? For example if I have this set up: Home About - Our great team - Why we are great Work - Photography - Design - ETC Contact How would I set a unique class for "home, about, work, contact"? And the child pages would get the same class as the parent page. For example "our great team" and "why we are great" would get the same unique class of "about". Link to comment Share on other sites More sharing options...
Marty Walker Posted March 1, 2013 Share Posted March 1, 2013 Couldn't you just do this? <body class="<?=$page->rootParent->name;"> 2 1 Link to comment Share on other sites More sharing options...
AnotherAndrew Posted March 2, 2013 Share Posted March 2, 2013 Thanks Marty. That gives me exactly what I needed! Just cleaning up your code a bit for anyone else who is wondering: <body class="<?php echo $page->rootParent->name;?>"> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now