ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

A semi-dynamic navigation question

May 14, 2009 7:26pm

Subscribe [4]
  • #1 / May 14, 2009 7:26pm

    Hey EE party people,

    I’m brand new to EE, and I’m really impressed so far. I’m building a new site - it’s essentially a mechanism for our client to capture and present the results of a body of research they have done. (That’s simplified, but should do for the point of this post, I hope.)  I’m wondering I could get some feedback on my data organization strategy and nav building technique.

    I’m thinking I’ll need 2 weblogs: 1) one with several text areas to hold general info about the site itself (title, introduction, purpose, description, etc); and 2) one with several text areas to hold info about the results of tests being run (conclusions, positives, negatives, quotes from test subjects, etc). There will be only one entry in the first weblog (there’s only one study), but there will be about 5 in the second (approx 5 test have ben run).

    I want 3 sections for the site: 1) Home page populated with some data from the first weblog; 2) Intro section with 4 subpages, all populated with data from the first weblog; 3) Testing results with a subpage for each test, each populated from an individual entry the second weblog. For navigation, I want a top level nav that shows those 3 sections, and a subnav for section 2 and 3.

    I ran into some difficulty making the subnavs for sections 2 and 3 becasue their built differently. Section 3 subnav was fairly easy b/c each page uses the same template populated by the different weblog entries. The following worked well (open list, select weblog, generate list items for each entry, close list)-

    <ul class="sub-nav">
        {exp:weblog:entries weblog="second"}
            <li><a href="http://{title_permalink=group/section-3-template}">{title}</a></li>
        {/exp:weblog:entries}
    </ul>

    So far so good. Client can even add or remove entries in the second weblog and subnav changes to match. Sweet!

    But it doesn’t seem so easy for Section 2’s subnav because each subpage holds only a subset of the first weblog’s single entry. Plus the sub pages are static. For client-based reasons, there will just be 4 defined pages pulling appropriate data from the first weblog. So I built this subnav thusly-

    <ul class="sub-nav">
        {exp:weblog:entries weblog="first"}
            <li><a href="http://{path=group/section-2-sub-1-template}">Sub 1</a></li>
            <li><a href="http://{path=group/section-2-sub-2-template}">Sub 2</a></li>
            <li><a href="http://{path=group/section-2-sub-3-template}">Sub 3</a></li>
            <li><a href="http://{path=group/section-2-sub-4-template}">Sub 4</a></li>
        {/exp:weblog:entries}
    </ul>

    Now that too works just fine - each subpage has it’s own template which pulls the appropriate data from the weblog.

    So with that long winded intro, my quesions to you all-

    1) Does this strategy make enough sense for you to say if that’s generally how you’d do it or not?
    2) All of the templates in Section 2 are the same, except that they each snag a different text field from the weblog. Seems like I should be able to have just one template with some logic to figure out with field to grab. Is there a way to pass a variable in the href link (ie “sub-1”) to work some logic in the template?

    Really enjoying working with EE. Thanks in advance for your time helping a newbie.

    -Willhaus

  • #2 / May 15, 2009 3:18am

    Brandon Allen

    17 posts

    1) For what you’re doing, your approach sounds sufficient.

    2) It is possible to use just on template. EE parses URLs into segments (URL Segment Variables), and then let’s you work with each segment, but calling it with the {segment} variable.

    In your case, I would do this with your section 2 subnav:

    <ul class="sub-nav">
        <li><a href="http://{path=group/section-2-template}/sub-1">Sub 1</a></li>
        <li><a href="http://{path=group/section-2-template}/sub-2">Sub 2</a></li>
        <li><a href="http://{path=group/section-2-template}/sub-3">Sub 3</a></li>
        <li><a href="http://{path=group/section-2-template}/sub-4">Sub 4</a></li>
    </ul>

    Since this subnav is static, and you’re not actually calling any weblog data to create this subnav, you can remove the weblog entries tag to avoid any unnecessary database calls.

    From there, you can do this to use one template to show one weblog field based on the URL segment:

    {exp:weblog:entries weblog="first"}
        {if segment_3 == "sub-1"}
            {custom_weblog_field_1}
        {if:elseif segment_3 == "sub-2"}
            {custom_weblog_field_2}
        {if:elseif segment_3 == "sub-3"}
            {custom_weblog_field_3}
        {if:elseif segment_3 == "sub-4"}
            {custom_weblog_field_4}
        {/if}
    {/exp:weblog:entries}
  • #3 / May 15, 2009 3:52am

    Whoa, totally. I get it. That’s a gigantic help.

    Gold star!

    Thanks a billion, Brandon.

    -Willhaus

  • #4 / May 15, 2009 1:12pm

    Oooooo. Even better. Since I’m only grabbing one text field per page, I gave the custom fields the same name as the segment. So rather than all that conditional logic, now my template only has the following-

    {{segment_3}}

    Awesome.

    Thanks again, Brandon.

    Willhaus

  • #5 / Jan 21, 2010 9:45pm

    Shane Robinson

    55 posts

    Oooooo. Even better. Since I’m only grabbing one text field per page, I gave the custom fields the same name as the segment. So rather than all that conditional logic, now my template only has the following-

    {{segment_3}}

    Awesome.

    This is so helpful and powerful in building reusable elements such as sidebars, footers, navigation, etc. where you want the content displayed based on URL segments but want to use a minimum of entries and shared templates.

    I added this to the SEGMENTS doc and it’s so powerful I believe it should be part of the official documentation.

  • #6 / Jan 22, 2010 3:49am

    Brandon Allen

    17 posts

    Glad I could help you two. Fir some reasoni didn’t get a notification email until Shane posted, so I’m sorry for disappearing on you willhaus.

  • #7 / Feb 03, 2010 4:51pm

    Todd D.

    460 posts

    Oooooo. Even better. Since I’m only grabbing one text field per page, I gave the custom fields the same name as the segment. So rather than all that conditional logic, now my template only has the following-

    {{segment_3}}

    Awesome.

    Thanks again, Brandon.

    Willhaus

    Interesting idea. I can’t help but wonder if that approach won’t come back and bite you at some point. Although, I can’t think of anything off the top of my head… it just seems like something will break…

    However, if this does work without any major consequences… I think it could help many ee developers.

    At any rate, I would like to see more discussion on it.

  • #8 / Feb 03, 2010 6:10pm

    Brandon Allen

    17 posts

    First off, I just realized how terrible my last response was. That’s what I get for trying to post from my phone.

    @Todd D.
    I agree with you. While I, also, can’t think of something off the top of my head, I can see this being a problem. Obviously, the conditional approach is safer, but Willhaus’ approach looks cleaner, and is a lot quicker. I guess, for now, you could always try it until it breaks. If it does break, however, you’re left to clean up a mess, or, worse, another developer is left to clean it up. I guess time will tell.

    To add a little extra safety against breakage, I might assign the segment to a variable. This way, you’re less likely to run into parsing issues/errors.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases