Unique boolean inside of a choice loop

Highlighted
New Member

Hi,

 

I have a custom coded email template that is using a choice field to display up to 10 unique blogs (each contains an image, title, leadin & link). I have a divider line running inbetween each loop to seperate the blogs.

 

Is there a way to add a boolean to be able to hide the divider line just on the last loop without having to build a custom module? Right now if I add the boolean around the divider, it show/hides all of the divider lines.

 

Below is the code I'm using.

 

{% set my_options = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] %}
{% choice "repeats1" label="Links We Love", value="1", choices="{{ my_options }}", export_to_template_context=True %}
{% for options in my_options %}
{% if loop.index <= widget_data.repeats1.value %} 
<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
    <tr>
        <td valign="top" align="center">
            <table width="181" border="0" cellspacing="0" cellpadding="0" align="left" class="em_wrapper" style="width:181px;">
                <tr>
                    <td height="5" class="em_hide" style="font-size:1px; line-height:1px;">&nbsp;</td>
                </tr>
                <tr>
                    <td align="center" valign="top" class="em_pad_bottom">{% linked_image "Image_1" label='Image_1', link="#", open_in_new_tab=True, alt='', src='https://cdn2.hubspot.net/hubfs/1879761/BBR%20eNews%20Template/1504707688513/image_1.jpg?noresize', width='181', style='border-width:0px;border:0px;width: 181px;display:block; max-width:181px; font-family:Arial, sans-serif; font-size:15px; line-height:20px; color:#000000; font-weight:bold;', unique_in_loop=True %}</td>
                </tr>
            </table>
            <table width="379" border="0" cellspacing="0" cellpadding="0" align="right" style="width:379px;" class="em_wrapper">
                <tr>
                    <td align="left" valign="top" class="em_gray em_align_cent" style="font-family:Arial, sans-serif; font-size:18px; line-height:27px; color:#4D4D4D; font-weight:bold;">{% rich_text "text_1" label="text_1", no_wrapper="True", html="This is a Really Long Blog Title That Drops to Two Lines", unique_in_loop=True %}</td>
                </tr>
                <tr>
                    <td height="8" style="font-size:1px; line-height:1px;">&nbsp;</td>
                </tr>
                <tr>
                    <td align="left" valign="top" class="em_gray em_align_cent" style="font-family:Arial, sans-serif; font-size:14px; line-height:22px; color:#4D4D4D;">{% rich_text "text_2" label="text_2", no_wrapper="True", html="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque hendrerit quam ut mauris mattis varius. Maecenas accumsan scelerisque bibendum.", unique_in_loop=True %}</td>
                </tr>
                <tr>
                    <td height="12" style="font-size:1px; line-height:1px;">&nbsp;</td>
                </tr>
                <tr>
                    <td align="left" valign="top" class="em_orange em_align_cent" style="font-family:Arial, sans-serif; font-size:15px; line-height:22px; color:#E87735; letter-spacing:3px;">{% rich_text "text_3" label="text_3", no_wrapper="True", html="READ MORE", unique_in_loop=True %}</td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td height="30" class="em_height" style="font-size:1px; line-height:1px;">&nbsp;</td>
    </tr>
    <tr>
        <td height="1" bgcolor="#ebebeb" style="font-size:1px; line-height:1px;">&nbsp;</td>
    </tr>
    <tr>
        <td height="30" class="em_height" style="font-size:1px; line-height:1px;">&nbsp;</td>
    </tr>
</table>
{% endif %}
{% endfor %}
Reply
0 Upvotes
1 Reply 1
HubSpot Moderator

Hi @tmolitor,

 

So I'll often run code in a loop every time except for the last by doing something like this:

 

{% if not loop.last %}
  {# code to run every time except for last #}
{% endif %}

But in your case it looks like you're limiting your loop by checking the loop index against widget_data.repeats1.value. Could you then wrap your bottom nested table inside a condition that checks for 1 less than widget_data.repeats1.value ? So that condition would look like this:

 

{% if loop.index < (widget_data.repeats1.value - 1) %}
  {# divider line #}
{% endif %}

I think that's right, but let me know if you have any questions about it.

 

Leland Scanlan

HubSpot Developer Support