2

I have a relationship field which contains a number of different pieces of content.

Each piece of content has a type property.

Is there anyway I can count the number of each of the types that is included in the relationship.

For example. 3 Product related items, and 2 Showcase related items.

I need to be able to get the {total_results} of each type.

This is what I'm trying to do...

 {if "{related:panel_type}" == "products"}

      {if "{related:count}" == "1"}
        <ul class="p">
      {/if}
        // do stuff 
      {if "{related:count}" == "{related:total_results}"}
        </ul>
      {/if}

    {/if}

  {if "{related:panel_type}" == "showcase"}

      {if "{related:count}" == "1"}
        <ul class="s">
      {/if}
        // do stuff 
      {if "{related:count}" == "{related:total_results}"}
        </ul>
      {/if}

    {/if}

So at the moment {total_results} is equal to 5 so my ul is not getting closed properly.

Does anyone have any ideas how I can get around this problem.

I would like to keep my markup inside the exp:channel:entries loop, and ideally use only one loop.

Any help, greatly appreciated

2

Faffing about with loop counters is quite unpleasant and inelegant... these are the kind of problems you run into when your data-fetching is tightly coupled to your data-output. Instead put Stash to work. Here's how:

{!--  ============================================
STORE THE DATA into dynamic stash lists using context.
Avoid the temptation to put any markup here.
================================================== --}

{exp:channel:entries ...}   
    {related}
        {exp:stash:append_list name="related-items" context="{related:panel_type}"}
            {stash:the-title}{related:title}{/stash:the-title}
            {stash:a-custom-field}{related:a-custom-field}{/stash:a-custom-field}
        {/exp:stash:append_list}
    {/related}
{/exp:channel:entries}


{!--  ============================================
ASSEMBLE THE MARKUP and OUTPUT THE DATA
================================================== --}

{exp:stash:parse process="end"}     

    {if {exp:stash:not_empty name="related-items" context="products"}}
        <h2>Related Products</h2>
        <ul class="p">
           {exp:stash:get_list name="related-items" context="products"}
             <li>{the-title} | {a-custom-field}</li>
           {/exp:stash:get_list}
        </ul>
    {/if}

    {if {exp:stash:not_empty name="related-items" context="showcase"}}
        <h2>Related Showcase</h2>
        <ul class="s">
           {exp:stash:get_list name="related-items" context="showcase"}
             <li>{the-title} | {a-custom-field}</li>
           {/exp:stash:get_list}
        </ul>
    {/if}

{/exp:stash:parse}

If you were using a separate view template for your layout you wouldn't need the final stash:parse tag, but if you're setting and getting in the same template it helps to avoid parse order issues. (Full explanation and tutorials here: http://www.jamessmith.co.uk/articles/expressionengine-stash-tutorials)

  • Hi James, thank you very much for your reply. I'll take a look at stash. Looks interesting. I'll mark this as resolved once I've had a play. Thanks again – magicspon Nov 30 '13 at 13:39
  • 1
    Just had the light bulb moment... :)... partials are belting! – magicspon Dec 2 '13 at 21:47

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.