If No Children, Display This Instead

Kristen Grote's Avatar

Kristen Grote

26 May, 2011 04:54 PM

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!

  1. Support Staff 2 Posted by Travis Schmeisser on 26 May, 2011 06:56 PM

    Travis Schmeisser's Avatar

    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.

  2. 3 Posted by Kristen Grote on 26 May, 2011 08:12 PM

    Kristen Grote's Avatar

    D'oh! I should have seen that. I expected it to be in the Tags section instead of the Docs section. Thanks!

  3. Travis Schmeisser closed this discussion on 26 May, 2011 08:28 PM.

  4. Kristen Grote re-opened this discussion on 15 Jun, 2011 03:40 PM

  5. 4 Posted by Kristen Grote on 15 Jun, 2011 03:44 PM

    Kristen Grote's Avatar

    Hey Guys,

    I have another question in the same vein. In the docs, this technique...

    {if structure:child_ids != ''}
    <div id="sweet_nav_wrapper">
    {exp:structure:nav mode="sub" start_from="{segment_1}"}
    </div>
    {/if}
    

    ...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!

  6. Support Staff 5 Posted by Travis Schmeisser on 15 Jun, 2011 03:53 PM

    Travis Schmeisser's Avatar

    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…

  7. 6 Posted by Kristen Grote on 15 Jun, 2011 03:55 PM

    Kristen Grote's Avatar

    not sure the exact instance for this, but if you are on a child page then the parent has children…

    I'm not sure what you're trying to say here...

  8. Support Staff 7 Posted by Travis Schmeisser on 15 Jun, 2011 04:00 PM

    Travis Schmeisser's Avatar

    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?

  9. 8 Posted by Kristen Grote on 15 Jun, 2011 04:17 PM

    Kristen Grote's Avatar

    Example Sitemap:

    Services
    About
    ---Mission Statement
    ---Careers
    ---Team
    Contact

    Here's my template code:

    {if structure:child_ids != ''}   
        <nav id="subnav">
            {exp:channel:entries limit="1"}
                <h2>{title}</h2>
            {/exp:channel:entries}
            {exp:structure:nav mode="sub" start_from="/{segment_1}"}
        </nav>
    {/if}
    

    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.

  10. Support Staff 9 Posted by Travis Schmeisser on 15 Jun, 2011 07:31 PM

    Travis Schmeisser's Avatar

    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?

  11. 10 Posted by Kristen Grote on 15 Jun, 2011 08:36 PM

    Kristen Grote's Avatar

    So why not have the parts you always want outside the conditionals and only the nav inside

    How is that different from what I have right now?

    or use multiple conditionals?

    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.

  12. Support Staff 11 Posted by Travis Schmeisser on 16 Jun, 2011 02:52 PM

    Travis Schmeisser's Avatar

    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.

  13. 12 Posted by Kristen Grote on 16 Jun, 2011 03:16 PM

    Kristen Grote's Avatar

    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!

  14. Support Staff 13 Posted by Travis Schmeisser on 16 Jun, 2011 03:27 PM

    Travis Schmeisser's Avatar

    Agreed, it's not ideal. We'll definitely look into this. Thanks!

  15. 14 Posted by Kristen Grote on 30 Jun, 2012 06:35 PM

    Kristen Grote's Avatar

    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.

  16. Support Staff 15 Posted by Travis Schmeisser on 01 Jul, 2012 05:34 PM

    Travis Schmeisser's Avatar

    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?

  17. 16 Posted by Kristen Grote on 01 Jul, 2012 07:28 PM

    Kristen Grote's Avatar

    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_ids or something like that.

    Make sense?

  18. Support Staff 17 Posted by Travis Schmeisser on 01 Jul, 2012 08:54 PM

    Travis Schmeisser's Avatar

    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.

  19. 18 Posted by Kristen Grote on 02 Jul, 2012 06:07 PM

    Kristen Grote's Avatar

    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.

  20. Support Staff 19 Posted by Travis Schmeisser on 02 Jul, 2012 07:18 PM

    Travis Schmeisser's Avatar

    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}' == ''}

  21. 20 Posted by Kristen Grote on 02 Jul, 2012 08:02 PM

    Kristen Grote's Avatar

    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"?

  22. Support Staff 21 Posted by Travis Schmeisser on 02 Jul, 2012 08:05 PM

    Travis Schmeisser's Avatar

    Yes

  23. 22 Posted by Kristen Grote on 02 Jul, 2012 08:09 PM

    Kristen Grote's Avatar

    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...

  24. Support Staff 23 Posted by Travis Schmeisser on 02 Jul, 2012 08:11 PM

    Travis Schmeisser's Avatar

    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.

  25. 24 Posted by Kristen Grote on 02 Jul, 2012 10:47 PM

    Kristen Grote's Avatar

    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?

  26. 25 Posted by Kristen Grote on 02 Jul, 2012 11:14 PM

    Kristen Grote's Avatar

    Got it!

    {if (structure:top:url == structure:page:url AND structure:child_ids != "") OR (structure:top:url != structure:page:url AND structure:child_ids == "") }
        <!-- Sidebar Code or Whatever -->
    {/if}
    

    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.

  27. Support Staff 26 Posted by Travis Schmeisser on 03 Jul, 2012 03:47 PM

    Travis Schmeisser's Avatar

    That is AWESOME! Thanks for sharing!

Comments are currently closed for this discussion. You can start a new one.

Recent Discussions

23 May, 2013 11:34 AM
23 May, 2013 08:22 AM
23 May, 2013 01:06 AM
22 May, 2013 09:05 PM