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.

Template Questions /

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

Custom variable for menu


Go to End


15 Posts   3613 Views

Avatar
Kamian

Community Member, 7 Posts

6 July 2015 at 8:49pm

Hey guys,

I really need help. I don't know much about SS and I'm in struggle with the menu.

Before I try to explain my issue, I just Copy & Paste my code.

<div class="dropdown 
<% control Menu(1) %>
	<% if LinkOrSection = section && Children %>
		<% if LinkingMode = current %>
			open
		<% else %>
			<% control Children %>
				<% if LinkOrSection = section && LinkingMode = current && Children %>
					open
				<% end_if %>
			<% end_control %>
		<% end_if %>
	<% end_if %>
<% end_control %>" id="leftnavigationDropdown">

All I want is to check if the current page have subpages. This would be ok if I don't use this again. But I do. So I repeat myself..

So here's my question: Can I define a variable in the backend to do this? Like "<% if HaveChildren %>open<% end_if %>"?

Avatar
Devlin

Community Member, 344 Posts

7 July 2015 at 12:57am

Like "<% if HaveChildren %>open<% end_if %>"?

A simple <% if $Children %>open<% end_if %> should do that.

// add class open if current page has children
<div class="dropdown <% if $Children %>open<% end_if %>" id="leftnavigationDropdown">

Please note, 'control' is deprecated. Use 'loop' or 'with' instead.

Avatar
Kamian

Community Member, 7 Posts

7 July 2015 at 2:02am

Hey, thank you for your answer. That's good but not perfect. Seems like I didn't explained my situation good enough.

So I got a Dropdown with 4 links in it. If I click on one of these links, I want to add in the dropdown class "open".
If I add "<% if $Children %>open<% end_if %>", the menu will always have "open" because the menu have 4 links. I just want to add "open" if the links have sublinks.

Avatar
Devlin

Community Member, 344 Posts

7 July 2015 at 2:32am

Edited: 07/07/2015 2:39am

No, the menu will have a "open" class if the current page has children.
If you click on another item in the menu which has no children, the Dropdown will not have a "open" class.

If I click on one of these links, I want to add in the dropdown class "open".

Ok, I see.

Avatar
Devlin

Community Member, 344 Posts

7 July 2015 at 2:53am

Edited: 07/07/2015 2:56am

So I guess in JavaScript, this would look something like this?

if( $('#leftnavigationDropdown .current, #leftnavigationDropdown .section').size() ) {
	$('#leftnavigationDropdown').addClass('open');
}

Avatar
Devlin

Community Member, 344 Posts

7 July 2015 at 3:24am

Edited: 07/07/2015 3:28am

Can I define a variable in the backend to do this? Like "<% if HaveChildren %>open<% end_if %>"?

Answering the original question. It's basically something like this:

class Page extends SiteTree {
	public function HaveChildren() {
		$output = false;
		// add logic to alter $output to true
		return $output;
	}
}

<div class="dropdown <% if HaveChildren %>open<% end_if %>" id="leftnavigationDropdown">

Avatar
Kamian

Community Member, 7 Posts

7 July 2015 at 6:34pm

I think thats it what I'm searching for. I'll try it. Thank you! (:

Avatar
Kamian

Community Member, 7 Posts

7 July 2015 at 11:27pm

How do I get the vars with the sitetree?

Go to Top