Blog, Website & Page Publishing

ben-duchy
Top Contributor

looping data rows

SOLVE

Is there any experts for loop conditions? I managed to loop my table data which works, but it's also repeating the title of the module.

 

{% set table = hubdb_table_rows(module.table_id, "&status=Available&orderBy=status&orderBy=class") %}

 

{% for status in table %}
{% if loop.length > 0 %}

 

<h2> module title </h2> <!-- needs to display only once and loop the slick divs -->

<div class="slick"><!-- slick content --></div>

 

{% for row in table %}

<!-- repeating rows based on availability filter -->

{% endfor %}

 

{% endif %}
{% endfor %}

 

If I move the title outside the if statement then it will appear regardless if there is zero data. Ideally I want the title to also hide if there's nothing in the module

0 Upvotes
1 Accepted solution
ben-duchy
Solution
Top Contributor

looping data rows

SOLVE

I've figured it out thanks to @Gonzalohttps://community.hubspot.com/t5/CMS-Development/For-loop-break/td-p/414

 

I've wrapped my title and main container in a loop.index

 

{% for status in table %}
{% if loop.index <= 1 %}
    <div class="module-bg">
        <div class="slick-wrapper">
            <h2 style="color: #baa569; text-align:center; margin: 0 0 30px 0;">{% inline_text field="module_title" value="{{ module.module_title }}" %}</h2>
            <div class="slick-house-types-v3">
{% endif %}
{% endfor %}

 

<!-- closing divs and slick carousel loops beneath this -->

 

This basically breaks the loop so that the main container with title only displays once whilst the content loops inside and if there is no content then the whole container disappears completely

View solution in original post

1 Reply 1
ben-duchy
Solution
Top Contributor

looping data rows

SOLVE

I've figured it out thanks to @Gonzalohttps://community.hubspot.com/t5/CMS-Development/For-loop-break/td-p/414

 

I've wrapped my title and main container in a loop.index

 

{% for status in table %}
{% if loop.index <= 1 %}
    <div class="module-bg">
        <div class="slick-wrapper">
            <h2 style="color: #baa569; text-align:center; margin: 0 0 30px 0;">{% inline_text field="module_title" value="{{ module.module_title }}" %}</h2>
            <div class="slick-house-types-v3">
{% endif %}
{% endfor %}

 

<!-- closing divs and slick carousel loops beneath this -->

 

This basically breaks the loop so that the main container with title only displays once whilst the content loops inside and if there is no content then the whole container disappears completely