Issue with repeater field

Hi guys!
I’m working on a landing page and i just made a repeater field with this code i found on the hubspot docs page.

{% set my_options = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] %}
{% choice "repeats" label="Number of modules to print", value="1", choices="{{ my_options }}", export_to_template_context=True %}
{% for options in my_options %}
 {% if loop.index <= widget_data.repeats.value %}
 {% linked_image "repeated_image" label="Repeated image", alt="Photo of Brian Halligan", src="//cdn2.hubspot.net/hub/53/file-733888619-jpg/assets/hubspot.com/about/management/brian-home.jpg", unique_in_loop=True %}
 {% text "repeated_caption" label="Repeated caption", value="Insert caption here", unique_in_loop=True %}
 {% endif %}
{% endfor %}

I have an issue with the labels i each iteration of the foor loop, it creates the fields correctly, but the labels are the same for every iteration, i tried to put the index of the loop inside the label of the custom module like this:

{% text "repeated_caption" label="Repeated caption-{{loop.index}}", value="Insert caption here", unique_in_loop=True %}

But when i go to edit the page it doesn’t work, is there anyway i can identify every label differently?
Thanks!

@mateomm22

I have a nifty trick for this. You will need to use block syntax for the modules inside of the loop (find it here in the docs).

{% widget_block rich_text "my_rich_text_module" overrideable=True, unique_in_loop=True %}
 {% widget_attribute "label" %}
 My rich-text module {{ loop.index }}
 {% end_widget_block %}
 {% widget_attribute "html" %}
 <h2>New Module</h2>
 <p>Add content here.</p>
 {% end_widget_attribute %}
{% end_widget_block %}

Notice 2 things:

1. unique_in_loop=true - makes the system name for the module unique

2. widget_attribute label - I broke the label loose using block syntax and set the loop index at the end to make it unique.
You should know that the ordering of modules using this method is utter chaos on the editor page. Instead of it display:

text 1

image 1

text 2

image 2

text 3

image 3

it will be more like:

image 3

text 1

text 2

image 1

text 3

image 2

It’s a bit of a mess. Makes for some pretty decent customization though.

Thanks for sharing @jsum! @mateomm22, please let us know if this solution helped you!

Awesome!
I’ll try it and i’ll tell you if that worked for me

Hi there!
Unfortunately this didin’t work, and somehow the last field I’m creating isn’t at the same order than the others

@mateomm22,

Can you show your code to help me understand what youa re talking about?

I’ve used this tons of tiems so it should work no problem.

{% set my_options = [1, 2, 3, 4, 5, 6] %}
{% choice "repeater" label="Número de destinos", value="2", choices="{{ my_options }}", export_to_template_context=True %}

{% for options in my_options %}
 {% if loop.index <= widget_data.repeater.value %}
 {% text "destino" label="Destino", value="Colombia", unique_in_loop=True, export_to_template_context=True %}
 {% widget_block text "precio" value="6 días - USD 1.250", unique_in_loop=True, export_to_template_context=True %}
 {% widget_attribute "label" %}
 Incluye {{ loop.index }}
 {% end_widget_attribute %}
 {% end_widget_block %}
 {% rich_text "fecha_salida" label="Fecha de salida", html='<p>Fecha de salida</p><p><strong>8 diciembre de 2017 a 4 de enero de 2018</strong></p>', unique_in_loop=True, export_to_template_context=True %}
 {% endif %}
{% endfor %}

I’m creating this loop, but it doesn’t print the label as you showed me

You were only applying the loop.index to the middle module in the loop. I refractored:

{% set my_options = [1, 2, 3, 4, 5, 6] %}
{% choice "repeater" label="Número de destinos", value="2", choices="{{ my_options }}", export_to_template_context=True %}

{% for options in my_options %}
 {% if loop.index <= widget_data.repeater.value %}
 {% widget_block text "destino" value="6 días - USD 1.250", unique_in_loop=True, export_to_template_context=True %}
 {% widget_attribute "label" %}
 Destino {{ loop.index }}
 {% end_widget_attribute %}
 {% end_widget_block %}
 {% widget_block text "precio" value="6 días - USD 1.250", unique_in_loop=True, export_to_template_context=True %}
 {% widget_attribute "label" %}
 Incluye {{ loop.index }}
 {% end_widget_attribute %}
 {% end_widget_block %}
 {% widget_block rich_text "fecha_salida" unique_in_loop=True, export_to_template_context=True %}
 {% widget_attribute "label" %}
 Fecha de salida {{ loop.index }}
 {% end_widget_attribute %}
 {% widget_attribute "html" %}
 <p>Fecha de salida</p><p><strong>8 diciembre de 2017 a 4 de enero de 2018</strong></p>
 {% end_widget_attribute %}
 {% end_widget_block %}
 {% endif %}
{% endfor %}

I tested and all of the modules have a number in the page editor

I don’t know what’s wrong with my code, but here is what the page editor is showing me.
I pasted the same code you gave me.

is it possible that you are updating the wrong template?

try creating a new landing page from the right template. I’ve confirmed the code works on my end, unless there is a caching issue or you are using the wrong template, I’m not sure why it isn’t working.

Hi @Jsum!
The solution you gave me has worked perfectly, but I have another question.

{% widget_block custom_widget "some_content" widget_name='Custom module Name', unique_in_loop=True, export_to_template_context=True %}
 {% widget_attribute "label" %}
 Custom label {{ loop.index }}
 {% end_widget_attribute %}
{% end_widget_block %}

For some reason the same code doesn’t work with custom modules inserted into the template, is there a way I can edit the label of the custom_widget module?
Thanks a lot!