CMS Development

stinhambo
Contributor

Looping %choice% bug when changing number of choices!

Hey all, I’ve found a bug in one of my templates (based on excellent work by @Andrew_Nimbler )
 
The template (see below) allows the user to choose a number of modules to display, then choose the modules from the corresponding choice fields, and finally outputs those choices on screen in the order you choose them.
 
The issue is that if I choose say 5 modules, populate them, then decide I want 7 modules, it somehow skips a few numbers in the loop.index so instead of outputting 1, 2, 3, 4, 5 (I haven’t chosen 6 or 7 yet) - it instead outputs 1,4,5,6,7 (see the 'Banner ' ~ loop.index0 variable - which means the 6th and 7th choice modules will populate the now vacant 2nd and 3rd place!
 
This isn’t good as it means the modules no longer correspond with my choice order - which means broken emails!Here is my code - can you figure out where I’m going wrong and how I could improve on this? -
 
 

 

{% set sections = range(1, 9) %}

{% set layout = '["Banner", "Highlight Section", "Full Width Content", "Two Column Content", "CTA"]' %}

{% choice "sections_field" label="How many content modules?", value="1", choices="{{ sections }}", export_to_template_context='True' %}

{% for section in sections|split(',', widget_data.sections_field.value) %}
    {% choice "layout_field" label="{{ 'Module number ' ~ loop.index }}", value="Choose a module", choices="{{ layout }}", export_to_template_context='True', unique_in_loop="True" %}
{% endfor %}

{% block modules %}
{% for widget in widget_data %}
{% if loop.index0 <= widget_data.sections_field.value %}
{% if widget.value == "Banner" %}
{% module "module_16183972633217" path="/Email/Modules/Banner_2021", label="{{ 'Banner ' ~ loop.index0 }}" no_wrapper="True" unique_in_loop="True" %}
        {% elif widget.value == "Highlight Section" %}
            {% module "module_161839738089114" path="/Email/Modules/Highlight Section_2021", label="{{ 'Highlight Section ' ~ loop.index0 }}" no_wrapper="True" unique_in_loop="True" %}
				{% elif widget.value == "Full Width Content" %}
            {% module "module_161839740102915" path="/Email/Modules/Full Width Content_2021", label="{{ 'Full Width Content ' ~ loop.index0 }}" no_wrapper="True" unique_in_loop="True" %}
				{% elif widget.value == "Two Column Content" %}
            {% module "module_161658827143752" path="/Email/Modules/Two Column Content_2021", label="{{ 'Two Column Content ' ~ loop.index0 }}" no_wrapper="True" unique_in_loop="True" %}
        {% elif widget.value == "CTA" %}
            {% module "module_161839735274213" path="/Email/Modules/CTA - Full Width_2021", label="{{ 'CTA ' ~ loop.index0 }}" no_wrapper="True" unique_in_loop="True" %}
	{% endif %}
{% endif %}
  {% endfor %}
{% endblock %} 

 

Screen Shot 2021-04-14 at 9.53.05 pm.png

 

 

0 Upvotes
10 Replies 10
John
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

Looping %choice% bug when changing number of choices!

Its hard to debug when we can't see the template. What does widget_data|pprint yield?

Is there a feature missing in the email drag and drop editor that you're trying to compensate for with this email template? You might can make a custom email module that will work in the drag and drop editor for each missing feature, then leverage the DnD editor for things like "full width" and "two column" options.

If the above is not an option, I'd honestly put it all in one module and skip the multi-module/separate choice field route – but I say that not knowing how many fields each module has and how complicated each module is in your original template.



I like kudos almost as much as cake – a close second.

0 Upvotes
stinhambo
Contributor

Looping %choice% bug when changing number of choices!

@John that is the template - I'm extending it with other HTML and modules don't contain anything that does `export_to_template_context`.

0 Upvotes
stinhambo
Contributor

Looping %choice% bug when changing number of choices!

The main features missing in the D&D editor is the lack of CSS control for things like dark mode, media queries, custom fonts and accessibility.

 

Adding non-inline CSS in the body is an absolute no no as Gmail will strip it out.

 

This doesn't really address the bug at hand, hopefully Hubspot can address this.

stinhambo
Contributor

Looping %choice% bug when changing number of choices!

Nope, can't figure this one out at all, anyone else see what's happening here? Am I missing something?

0 Upvotes
John
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

Looping %choice% bug when changing number of choices!

The line below inside the for loop is skipping over anything not equal to "Banner". I think that may be the culprit.

{% if widget.value == "Banner" %}

 



I like kudos almost as much as cake – a close second.

stinhambo
Contributor

Looping %choice% bug when changing number of choices!

Wouldn't it go to the elif statements next?

0 Upvotes
John
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

Looping %choice% bug when changing number of choices!

haha you're exactly right. The way the code was indented I just skipped over it. Sorry
You should pretty print widget_data. I have a feeling there are some extra modules in there that are getting skipped over. Looking at that data might give you some answers

{{ widget_data|pprint }}

 



I like kudos almost as much as cake – a close second.

stinhambo
Contributor

Looping %choice% bug when changing number of choices!

It looks like the issue occurs as soon as I choose to have more sections in sections_field (6 or above) than I have options in the layout variable (five choices).

Still investigating!

0 Upvotes
dennisedson
Community Manager
Community Manager

Looping %choice% bug when changing number of choices!

Hey @stinhambo ,

Do you have a link to the template in the marketplace?

Wondering if he has a support email for things like this

Thanks,

Dennis




HubSpot Community Developer ShowMake sure to subscribe to our YouTube channel
where you can find the HubSpot Community Developer Show
0 Upvotes
stinhambo
Contributor

Looping %choice% bug when changing number of choices!

No this is somethink I created myself for an email template.

0 Upvotes