If No Children, Display This Instead
Hey there,
I'm trying to streamline a couple of templates, and I was wondering if there was a way to make a conditional based on whether or not the page has children. Something like:
{if structure_children}
{exp:structure:nav mode="sub" include_ul="no" start_from=parent}
{if:else}
<p>This page has no children</p>
{/if}
Possible or just a pipe dream? Thanks!
Comments are currently closed for this discussion. You can start a new one.
Support Staff 2 Posted by Travis Schmeisser on 26 May, 2011 06:56 PM
Look in our docs, there's one for conditionally displaying content and nav. Sorry I'm away from the computer and don't have it handy.
3 Posted by Kristen Grote on 26 May, 2011 08:12 PM
D'oh! I should have seen that. I expected it to be in the Tags section instead of the Docs section. Thanks!
Travis Schmeisser closed this discussion on 26 May, 2011 08:28 PM.
Kristen Grote re-opened this discussion on 15 Jun, 2011 03:40 PM
4 Posted by Kristen Grote on 15 Jun, 2011 03:44 PM
Hey Guys,
I have another question in the same vein. In the docs, this technique...
...hides the sub navigation if the current page has no children. Is there a way to hide the subnavigation only if the parent page has no children?
Thanks as always for your awesome support!
Support Staff 5 Posted by Travis Schmeisser on 15 Jun, 2011 03:53 PM
We currently do not have a parent child ids variable - not sure the exact instance for this, but if you are on a child page then the parent has children…
6 Posted by Kristen Grote on 15 Jun, 2011 03:55 PM
I'm not sure what you're trying to say here...
Support Staff 7 Posted by Travis Schmeisser on 15 Jun, 2011 04:00 PM
I'm saying I'm not sure exactly what the use case for this is, but I don't understand. If you are on a child page, why would you be checking if the parent has children?
8 Posted by Kristen Grote on 15 Jun, 2011 04:17 PM
Example Sitemap:
Services
About
---Mission Statement
---Careers
---Team
Contact
Here's my template code:
So I have some code that is part of the overall visual style of the navigation, but is not part of the Structure subnav tag.
With this code, if I navigate to the 'About' parent page, the sub-navigation shows up fine, because it has children. But if I go to the 'Team' child page, the navigation disappears, I assume because that current page has no children.
Now, some of my top-level pages, like 'Contact' and 'Services', don't have any children, so I obviously don't want to display the chunk of navigation code at all.
That was pretty long-winded, but I hope it makes sense.
Support Staff 9 Posted by Travis Schmeisser on 15 Jun, 2011 07:31 PM
So why not have the parts you always want outside the conditionals and only the nav inside or use multiple conditionals? Am I misunderstanding something?
10 Posted by Kristen Grote on 15 Jun, 2011 08:36 PM
How is that different from what I have right now?
What additional conditionals would I use?
Just to clarify, the
{if structure:child_ids != ''}technique will display the sub nav only if the current page has children right? Regardless of whether the current page is the child of another page? I'm just making sure what I'm experiencing is expected behavior.My ultimate problem is that the sub navigation is only displaying on parent pages, but disappears on child pages. I want the sub nav to display on both the parent pages and the child pages, but not display if the page is top-level only.
Support Staff 11 Posted by Travis Schmeisser on 16 Jun, 2011 02:52 PM
Ahhh I see. We don't have a current variable for that. I think you'll have to use segment checking and change what subnav code you show based off the current section. I'll add that variable to our list though, just no timeframe to share yet.
12 Posted by Kristen Grote on 16 Jun, 2011 03:16 PM
OK sounds good. Obviously segment checking is limited since it doesn't allow for new top-level pages to be created, which is why I was hoping Structure had a solution for me. But no worries, I appreciate all your hard work, thanks!
Support Staff 13 Posted by Travis Schmeisser on 16 Jun, 2011 03:27 PM
Agreed, it's not ideal. We'll definitely look into this. Thanks!
14 Posted by Kristen Grote on 30 Jun, 2012 06:35 PM
Did any functionality for this ever get added? Reading back over it my explanation sounds really confusing.
Basically I want to display the title of a top-level page ONLY IF it has children. If it doesn't have children, I don't want to display the title.
Support Staff 15 Posted by Travis Schmeisser on 01 Jul, 2012 05:34 PM
We do this different than edited because that wasn't as flexible.
Here's out doc article: http://buildwithstructure.com/documentation/conditionally_display_sub_navigation_or_html/
Does that help?
16 Posted by Kristen Grote on 01 Jul, 2012 07:28 PM
That only works if the current page has children.
I'm basically trying to display an overview title in the sidebar of all pages. But I don't want the overview title to display if it's a single, top-level page with no children.
So I guess the preferred variable would look like
parent_child_idsor something like that.Make sense?
Support Staff 17 Posted by Travis Schmeisser on 01 Jul, 2012 08:54 PM
Can you do a segment based extra conditional then (mapped to the parent's segment)? We're going to work on a parent_child_ids or sibling variable or something like that soon.
You could prob also do a check against the sibling tag to see if it's empty.
18 Posted by Kristen Grote on 02 Jul, 2012 06:07 PM
I'm not sure what you mean by a "segment based extra conditional mapped to the parent's segment". What would that look like?
I'm also not sure how checking if a page has siblings would work either.
Support Staff 19 Posted by Travis Schmeisser on 02 Jul, 2012 07:18 PM
I mean adding a check like {if segment_2 =="news"} or maybe add in {if '{exp:structure:siblings}{prev}{url}{/prev}{next}{url}{/next}{exp:structure:siblings}' == ''}
20 Posted by Kristen Grote on 02 Jul, 2012 08:02 PM
Ah, ok. Yeah, the segment-based checking is less than ideal since it doesn't allow for new solo, top-level pages to be created.
And the sibling checking, is that saying: "if this page does not have any siblings, then display this code"?
Support Staff 21 Posted by Travis Schmeisser on 02 Jul, 2012 08:05 PM
Yes
22 Posted by Kristen Grote on 02 Jul, 2012 08:09 PM
OK, so how would the sibling checking work to display code on parent pages? Are top-level pages considered siblings with each other? Even so, I'm not sure how that would affect solo, childless pages.
I apologize if I'm being thick...
Support Staff 23 Posted by Travis Schmeisser on 02 Jul, 2012 08:11 PM
You may need to do some nested conditionals or If;else - it all depends what you need. You would check again child_ids for a parent, but use the sibling conditional for when you are on one of it's children. Make sense? Sometimes it helps to write out actual text for what you are trying to do, then start replacing with variables and forming the logic.
24 Posted by Kristen Grote on 02 Jul, 2012 10:47 PM
Hmm, I'm trying to figure out how to differentiate a solo, top-level page from a child page with siblings.
Both do not have children.
Both have siblings.
Both have parents (according to structure, anyway).
So what makes the two different from each other in structure's eyes?
25 Posted by Kristen Grote on 02 Jul, 2012 11:14 PM
Got it!
Basically saying:
If this page is the same as the top-level page AND this page has children, display this code.
OR
If this page is not the same as the top level page AND this page does not have children, display this code.
The solo, childless pages will not match these parameters because they ARE the same as the top level, but they do NOT have children.
Boo yeah.
Support Staff 26 Posted by Travis Schmeisser on 03 Jul, 2012 03:47 PM
That is AWESOME! Thanks for sharing!