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.

Bug (?) using embed to nest {exp:channel:categories}

September 12, 2012 11:13am

Subscribe [1]
  • #1 / Sep 12, 2012 11:13am

    ChiefAlchemist

    913 posts

    I have a category group with parents and each of those has one level of children. I want to display the parent and then the children, parent then children and so on. However, for the children I want to use ’ :: ’ as a separator, which means (I presume) I need to use backspace. But I want to use backspace each time I get done with a loop of children and not just at the end of a single {exp:channel:categories} loop.

    Therefore, I split the code into to with the children being done with an embed. The outer loop displays the parents and the inner loop (via an embed) lists the children for that parent. Piece of cake, right? Well, not really.

    => Here’s the code to display the parents:

    <ul>
      {exp:channel:categories channel='my-cats' category_group='5'  parent_only='yes' style='linear'}
       <li class="tour-season"><a href="http://{path=/{category_url_title}}">{category_name}</a></li>
    <ul>{embed='embeds/.tour_dates_list_events' parent='{category_id}'}</li></ul>
      {/exp:channel:categories}
       </ul>

    => Here’s the code of the embed to display the children:

    {exp:channel:categories channel='my-cats' category_group='5' style='linear' backspace='16'}
    {if parent_id == {embed:parent}}<li class="tour-event"><a href="http://{path=/{segment_1}/{category_url_title}}">{category_name}</a> :: </li>
    {/if}{/exp:channel:categories}

    For some reason the if statement seems to add returns or something if the conditional is not true. In other words, the backspace only works for the last set of children. For anything prior to the last set the backspace count isn’t right and for all practical purposes there is no backspaces removed - at least not as many as have been added.

    Even the final children loop it’s not right. I figured need 10 spaces to remove the last ’ :: </li> ’ (9 + a return) but that doesn’t work right either.

    Note: I even tried to remove all the returns in the code in embed for the children. That is, it was one single long line of code. That didn’t work either. I still picked up characters that I didn’t want (and could not see).

    What am I not grasping about how EE thinks? Or is there a parsing issue in EE?

    In either case, how can I do what I need to do? That is, Parent + list of children with separator; Parent + list of children with separator; and so on…

     

  • #2 / Sep 14, 2012 2:38pm

    Dan Decker

    7338 posts

    Hi ChiefAlchemist,

    Well - how about the style=“nested” parameter? It does exactly what you want, with your desired output.

    <ul id="nav_categories">
      <li>News
        <ul>
          <li>Regional</li>
          <li>World</li>
        </ul>
        </li>
      <li>Sports
        <ul>
          <li>National
            <ul>
              <li>Football</li>
              <li>Basketball
                <ul>
                  <li>Lakers</li>
                  <li>Knicks</li>
                </ul>
                </li>
            </ul>
            </li>
        </ul>
        </li>
    </ul>

    That automatically builds an unordered list of patents with an unordered list of children right below.

    ~

    Edit - dang - that doesn’t account for your special end item - although you could target the child <li> element with CSS and add the “::” that way.

  • #3 / Sep 20, 2012 3:51pm

    ChiefAlchemist

    913 posts

    Thanks Dan.

    I should have added that I’m using categories in a semi-unconventional way. They don’t define blog post, but pages and I suppose for this discussion sub-pages.

    As a result, the parents have one URL structure and the children something different.

    More of less

    /Parent 1/
    - /parent-1/child-a
    - /parent-1/child-b
    - /parent-1/child-c

    /Parent 2/
    - /parent-2/child-a
    - /parent-2/child-b
    - /parent-2/child-c

    The parents are Years and have a certain template. The children are Events within that Year and have their own template structure.

    I was under the impression (read: I thought I tried it) that nested presumes the URL structure is consistent across all categories.

  • #4 / Sep 20, 2012 3:54pm

    ChiefAlchemist

    913 posts

    I think you might have to re-open that other thread I started. While I’m trying to solve the same problem (in a different way), that one feels like a bug to me.

    I agree. One at a time 😊 But let’s no lose try of the one that seems strange.

    Thx again.

  • #5 / Sep 24, 2012 12:01pm

    Dan Decker

    7338 posts

    Hi Chief,

    I was under the impression (read: I thought I tried it) that nested presumes the URL structure is consistent across all categories.

    Hrm… yeah, you would need a conditional:

    {exp:channel:categories channel='my-cats' category_group='5'  parent_only='yes' style='linear'}
      {if parent_id == 0}
        <a href="http://{path=/{category_url_title}}">{category_name}</a>
      {/if}
      <a href="http://{path=/{segment_1}/{category_url_title}}">{category_name}</a>
    {/exp:channel:categories}

    That should do it.

    Hit me back with a link to that closed thread and I’ll take a 2nd look at it.

    Cheers,

  • #6 / Sep 24, 2012 2:55pm

    ChiefAlchemist

    913 posts

    Hey Dan

    Well, yeah, per my OP that’s what I was doing (more or less). But for some reason I was picking up added spaces. Which in turn screwed up using backspace.

    fyi - the client has since changed the spec so i don’t need a resolution, that said, i’m sure it’ll kinda come up again soon-ish so i do want an answer. if there’s no resources just close this and i’ll wait for it to pop up again.

  • #7 / Sep 25, 2012 11:47am

    Dan Decker

    7338 posts

    But for some reason I was picking up added spaces.

    What version of ExpressionEngine is this?

    Going back over this again, and the fact that it is likely to come up again, I’d like to replicate.

    Thanks,

  • #8 / Sep 28, 2012 8:23am

    ChiefAlchemist

    913 posts

    => v2.5.2 - Build Date: 20120606

    dev - running on dreamhost

    prod - blue host (tho’ I’m not sure if I’ve test this extra spaced on prod)


    => Also, and I only mention this because it might be related (*maybe), I’m pickup up extra spaces / returns from preload_replace. I use them to write the code (if you will) on the fly and have a block of them (8 to 10+ lines of them) at the top of my index. When I view source, I see 8 to 10 empty lines. Harmless but none the less odd.

    Thanks again for your help. Let let me know if you need anything else.

  • #9 / Sep 28, 2012 4:06pm

    Dan Decker

    7338 posts

    Hey Chief,

    I suspect what we are seeing is related to this:

    Backspacing removes characters (including spaces and line breaks)...

    What reminded me of this is when you said:

    ... have a block of them (8 to 10+ lines of them) at the top of my index. When I view source, I see 8 to 10 empty lines.

    EE is seeing the line-breaks from the snippet code, even if it isn’t being rendered.

    The same holds true with the embeds that aren’t evaluating, the line-breaks are being added, which makes the backspace inconsistent.

    The (ugly) solution would be to include all the code for the embeds and snippets on one line with no rendered spaces. The embed would also need to be on a line left-justified to prevent tab characters from being repeated and counted.

    ~

  • #10 / Sep 28, 2012 5:57pm

    ChiefAlchemist

    913 posts

    Hey Dan

    The large empty block is from preload_replace.

    I tried pulling out all the returns + spaces and still couldn’t find a pattern for the results.

    You can close it out. I’m not sure what it is. But it is what it is.

    Love EE. Thanks again!

    Mark

  • #11 / Sep 30, 2012 8:37am

    ChiefAlchemist

    913 posts

    fyi - I just double checked and preload_replace is adding a return in my

    <head> </head>

    .

    I don’t think that’s related to this problem per se. But per my other thread I was picking up unanticipated spaces (returns?) that were screwing up backspace when I was trying to list my cats that other way.

    Just sayin’ 😉

    Thanks again -

    Mark

  • #12 / Oct 01, 2012 3:23pm

    Dan Decker

    7338 posts

    Hi Mark,

    Thank you for using ExpressionEngine.

    Is there anything else I can assist you with?

    Cheers,

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

ExpressionEngine News!

#eecms, #events, #releases