Developer forum

Forum » Templates » Test if multiple loops is defined

Test if multiple loops is defined

Anders Ebdrup
Reply

Hi Dw,

 

Is it possible to test if one or more of multiple loops is defined?

 

Say

    <div class="related-products row">
        <!--@LoopStart(Related.AssociatedProducts)-->
        <!--@HeaderStart-->
        <div class="associated-products col-sm-4">
            <ul class="list-unstyled">
                <!--@HeaderEnd-->
                <li>
                    <a href="<!--@Ecom:Product.Link.Clean-->">
                        <!--@Ecom:Product.Name-->
                    </a>
                </li>
                <!--@FooterStart-->
            </ul>
        </div>
        <!--@FooterEnd-->
        <!--@LoopEnd(Related.AssociatedProducts)-->

        <!--@LoopStart(Related.HandbookContainsProducts)-->
        <!--@HeaderStart-->
        <div class="handbook-contains col-sm-4">
            <ul class="list-unstyled">
                <!--@HeaderEnd-->
                <li>
                    <a href="<!--@Ecom:Product.Link.Clean-->">
                        <!--@Ecom:Product.Name-->
                    </a>
                </li>
                <!--@FooterStart-->
            </ul>
        </div>
        <!--@FooterEnd-->
        <!--@LoopEnd(Related.HandbookContainsProducts)-->
    </div>

Is it here possible to test if: "Related.AssociatedProducts" or "Related.HandbookContainsProducts" if defined before outputting the containing div.related-products?

 

Best regards, Anders


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi Anders,

 

You can use LoopDefined: http://templates.dynamicweb-cms.com/TemplateTags/Dynamicweb-template-tags/General-tags/If-LoopDefined.aspx

 

Also, the Header and Footer won't be rendered if there are no items in the collection.

 

Cheers,

 

Imar

 

 
Anders Ebdrup
Reply

 

Original message by Imar Spaanjaars posted on 05/12/2013, 22:10:

Hi Anders,

 

You can use LoopDefined: http://templates.dynamicweb-cms.com/TemplateTags/Dynamicweb-template-tags/General-tags/If-LoopDefined.aspx

 

Also, the Header and Footer won't be rendered if there are no items in the collection.

 

Cheers,

 

Imar

 

Thanks for very quick reply, Imar! :-)

 

But is it possible to test for "OR" in the LoopDefined? I need to know if at least of the my loops contains items.

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

I don't think you can do that directly. However, you can nest the If checks giving you the desired results....

 

Imar

 

 
Anders Ebdrup
Reply

 

Original message by Imar Spaanjaars posted on 05/12/2013, 22:10:

I don't think you can do that directly. However, you can nest the If checks giving you the desired results....

 

Imar

 

But if I nest the If-statements, then I have a problem if the first one is false and then it will never get to the second one...

 

Anders

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

If the first one is false, you can check for the other one in the Else statement.  If the first is True you can check for the other one for true or false depending on your requirements. This may lead to duplicate code, but you can work around that using Includes:

 

http://templates.dynamicweb-cms.com/TemplateTags/Dynamicweb-template-tags/General-tags/If-statements.aspx

http://templates.dynamicweb-cms.com/TemplateTags/Dynamicweb-template-tags/General-tags/Include%28Path%29.aspx

 

Cheers,

 

Imar

 

 
Morten Bengtson
Reply
This post has been marked as an answer

If you only need this to display loops of related products (from RelatedProductListProvider) then you should be able to check the count on multiple collections like this...

<!--@If(eCom:Related.MostPopularProducts.Count>0||eCom:Related.YouHaveSeenTheseProducts.Count>0)-->
YES - At least one of the loops contain related products. Render your loops and div wrapper here.
<!--@Else-->
NO - there are no related products to display.
<!--@EndIf-->

 

 

Votes for this answer: 1
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Nice solution!

 

Imar

 

 
Anders Ebdrup
Reply

 

Original message by Morten Bengtson posted on 05/12/2013, 22:10:

If you only need this to display loops of related products (from RelatedProductListProvider) then you should be able to check the count on multiple collections like this...

1
2
3
4
5
<!--@If(eCom:Related.MostPopularProducts.Count>0||eCom:Related.YouHaveSeenTheseProducts.Count>0)-->
YES - At least one of the loops contain related products. Render your loops and div wrapper here.
<!--@Else-->
NO - there are no related products to display.
<!--@EndIf-->

 

 

Wauw! Thanks a lot. Works like a charm!

 

You must be logged in to post in the forum