Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo

Contextual Body Ids


Go to End


3 Posts   1901 Views

Avatar
chrisjlee

Community Member, 5 Posts

20 November 2008 at 9:22am

I'm actually an avid Wordpress hack (very new to silverstripe and liking a lot of it) and my favorite thing was being able to change layouts/columns on the fly with primarily css. In order to do that I would require the body tag to adapt different Id's and classes attached. I've tried doing a forum search; but nothing that satiates my quest for an answer.

I had a discussion with a colleague about adapting and creating the same functionality in a template. We came up with some solutions but was thinking maybe there's a more elegant approach.

1. Use the $title and place it where the body tag and have SS parse it there
2. Use conditional level modifiers <% if level ( 0 ) % > to create different id's. Eventually it would output something like <body class="level-1">

If anyone could offer a more elegant solution i'd love to document it in the wiki.

Just a caveat: i'm i just started learning the ins and outs of Silverstripe. I apologize if there are any egregious statements. There are probably many ha.

Avatar
Liam

Community Member, 470 Posts

20 November 2008 at 2:29pm

Generally people use the $URLSegment variable in the body tag. It returns the url part after the domain name, i.e the url segment lol.

This would be pretty much the same thing as using the $title in there, except it would probably be shorter and no spaces, only dashes. About Us = about-us

That's probably the best way to do it via CSS. You can assign specific templates for certain page types by creating new page types. Also, the $Layout variable works for good chunks of good.

Hammish just wrote up a good explanation for a new user which further elobrates on the docs.

http://www.silverstripe.com/site-builders-forum/flat/231937#post231943

Also be sure to checkout the built in page controls if you haven't already.
http://doc.silverstripe.com/doku.php?id=built-in-page-controls

Avatar
Willr

Forum Moderator, 5523 Posts

20 November 2008 at 7:57pm

Yeah normally $URLSegment works well for doing different styles for different pages provided that you dont need to change the URL too often :P

<body id="$URLSegment">