HubL question: looping {% choice %} & using {% if %} to detect each option
SOLVE
I'm looking into creating a master email template that contains multiple layouts that the client can show/ hide. The idea is that the template is locked down so the client can't do anything unintended, but allows them to create all their email communication without ruining their template's visual style. Similar to how Campaign Monitor allows you to repeat layouts you define in the template.
I'm using HubL to create a choice that selects how many times to repeat another choice that allows the client to pick a unique layout for each of the repeats.
Below is the code I'm using which doesn't quite work, as it doesn't repeat the second choice field.
{% choice "sections_field" label="How many sections?", value="1", choices="1, 2", export_to_template_context=True %}
{% for sections in widget_data.sections_field.value %}
{% choice "layout_field" label="Use layout?", value="One Column", choices="One Column, Two Columns", export_to_template_context=True, unique_in_loop=True %}
{% if widget_data.layout_field.value == 'One Column' %}
One Column
{% elif widget_data.layout_field.value == 'Two Columns' %}
Two Columns
{% endif %}
{% endfor %}
Any ideas? I can do all this manually in the template by just copying the choice a few times, but I am hoping to get the loop working as it would be easier to maintain...
HubL question: looping {% choice %} & using {% if %} to detect each option
SOLVE
I would recommend against using the repeater option in an email template at this time with what you are trying to do. I have tried this in the past and found it a pain mainly because the repeater elements when placed on the page will not match up with the element list in the edit menu. I submitted a ticket about this about a year ago and was told support had no plans to fix the issue. I am uploading a pdf and image of the support ticket here so you can see. I removed and blurred pieces so not to throw the support person under the bus or anything like that.
I basically was able to accomplish this with a really long IF statement in order to be able to get it to function as needed and allow for the user to be able to use either the edit menu or the clicking directly on the module to edit area in the same order.
Until HS can fix their repeater module, this will continue to be an issue.
Feb 18, 20196:56 AM - last edited on Sep 15, 20209:46 AM by jennysowyrda
Member
HubL question: looping {% choice %} & using {% if %} to detect each option
SOLVE
Thanks for giving us so much time !! Great tips man! Worth reading. To be honest at first sight I thought you’ll use the same old tips people used to write on their blogs, but here I learnt some new things in this key Point, something out of the box!
HubL question: looping {% choice %} & using {% if %} to detect each option
SOLVE
What are you trying to accomplish by doing this line? You are trying to loop through one value right there. I'm not sure what exactly you are doing, but maybe try without the .value
{% for sections in widget_data.sections_field.value %}
HubL question: looping {% choice %} & using {% if %} to detect each option
SOLVE
Thanks for the feedback, your right nickdeckerdevs, I wasn't understanding the for loop correctly. I've updated the for loop & now I'm having a problem with the if statements not detecting each unique choice per loop.
{% set sections = [1, 2, 3, 4] %}
{% set layout = '["One Column", "Two Columns"]' %}
{% choice "sections_field" label="How many sections?", value="1", choices="{{ sections }}", export_to_template_context=True %}
{% for section in sections|split(',', widget_data.sections_field.value) %}
{% choice "layout_field" label="Use layout?", value="One Column", choices="{{ layout }}", export_to_template_context=True, unique_in_loop=True %}
{% if widget_data.layout_field.value == 'One Column' %}
One Column
{% elif widget_data.layout_field.value == 'Two Columns' %}
Two Columns
{% endif %}
{% endfor %}
If I use 'widget_data.layout_field_2.value' as an if statement it gets the second choice's value, but I can't figure out how to add index.loop into the variable to get it to treat each variable in the for loop as unique.
HubL question: looping {% choice %} & using {% if %} to detect each option
SOLVE
This is a perfect and easy solution to work with choice fields. But if you will work with special characters like ñ,{, }, [, ], ô, ü, etc. I recommend to you change each option in a value + label combo: