0

I need to pass a list of the first level of categories to a nested switchee. The following works when I manually place the nested list. However I would like the client to be able to add/edit categories.

{exp:switchee variable="{segment_2}" parse="inward"}
  {case value="#^P(/d+)$#|category|''"}

    {switchee variable="{segment_3}" parse="inward"}

      {case value="walls|seating|workstation|training|private-offices|ergo|meeting-media-room"}{!--level 1 category listing--}
        first level category listing here
      {/case}{!--level 1 category listing--}

      {case default="yes"}{!--level 2 category listing--}

        2nd level category listing here

      {/case}{!--level 2 category listing--}

    {/switchee}{!--end nested switchee--}


  {/case}{!--listing & category listing--}

  {case value="value" default="yes"}
    single entry here
  {/case}{!--single--}
{/exp:switchee}

When I replace the case value list with the code below it doesn't work. I'm sure it's something to do with parse order, but am stuck. If I place the code below elsewhere in the template it outputs what I need.

{exp:gwcode_categories channel="products" depth="1" style="linear" backspace="1"}{cat_name}|{/exp:gwcode_categories}

Possibly this could be done with Stash, but I've never been able to grok how to use stash.

2

With Stash:

{exp:stash:set name="categories" parse_tags="yes"}{exp:gwcode_categories channel="products" depth="1" style="linear" backspace="1"}{cat_name}|{/exp:gwcode_categories}{/exp:stash:set}

{exp:stash:parse}
  {exp:switchee variable="{segment_2}" parse="inward"}
    {case value="#^P(/d+)$#|category|''"}

      {switchee variable="{segment_3}"}

        {case value="0|{stash:categories}"}{!--level 1 category listing--}
          first level category listing here
        {/case}{!--level 1 category listing--}

        {case default="yes"}{!--level 2 category listing--}

          2nd level category listing here

        {/case}{!--level 2 category listing--}

      {/switchee}{!--end nested switchee--}

    {/case}{!--listing & category listing--}

    {case value="value" default="yes"}
      single entry here
    {/case}{!--single--}
  {/exp:switchee}
{/exp:stash:parse}
| improve this answer | |
  • Mark this works, one question why is the case using "0" instead of "''"? – CreateSean Jun 6 '14 at 11:34
  • No reason, it could be that if you wanted to match no value too. – Mark Croxton Jun 6 '14 at 13:37
1

parse="inward" is the villain here. Switchee is being parsed before the exp:gwcode_categories.

Since I believe you need the parameter, your option is to force exp:gwcode_categories to be parsed early. Let's try using embeds.

{embed="group/template" categories="{exp:gwcode_categories channel="products" depth="1" style="linear" backspace="1"}{cat_name}|{/exp:gwcode_categories}"}

Then, on embed:

{exp:switchee variable="{segment_2}" parse="inward"}
  {case value="#^P(/d+)$#|category|''"}

    {switchee variable="{segment_3}"}

      {case value="{embed:categories}"}{!--level 1 category listing--}
        first level category listing here
      {/case}{!--level 1 category listing--}

      {case default="yes"}{!--level 2 category listing--}

        2nd level category listing here

      {/case}{!--level 2 category listing--}

    {/switchee}{!--end nested switchee--}

  {/case}{!--listing & category listing--}

  {case value="value" default="yes"}
    single entry here
  {/case}{!--single--}
{/exp:switchee}

There's yet some room for improvement here. For example, you're checking for segment_3 even if segment_2 is empty.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.