CMS Development

vin-aws
投稿者

Module unique_in_loop inside nested for loop

I currently have a nested for loop and it seems like module parameter unique_in_loop only works for the inner most loop. I would like it to be unique in respect to both loops. Is there any workaround to this?

 

Example: This will return 3 unique text modules that will be the same 3 for each outer loop iteration.

 

{% for i in range(3) %}
    {% for i in range(3) %}
        {% text "my_text" no_wrapper=True, unique_in_loop=True %}
    {% endfor %}
{% endfor %}
0 いいね!
4件の返信
Jsum
キーアドバイザー

Module unique_in_loop inside nested for loop

Tricky.

 

I'm not sure why you would want to have 3 sets of text modules that all output the same text, but as long as your aware that this is the case I might have a solution.

 

unique_in_loop simply adds to the module's system name to make it unique for each iteration of a loop. modules with the same system name.  I can't think of a way off the top of my head that would allow you reference an outer loop from inside a nested loop, but since you want the same module repeated in the nested loop, but unique to the outer loop, you need to create the module in the outer loop. 

 

I got this far, but am currently stuck:

 

{% set num = 3 %}
      
      {% for i in range(num) %}
      	
        {% if loop.index > 1 %}
      		{% set unique = "_" + loop.index %}
      	{% else %}
      		{% set unique = "" %}
      	{% endif %}
      	
      	{% set texter = "widget_data.text" ~ unique ~ ".value" %}
      
      	{% widget_block text 'text' label="text", unique_in_loop=True, export_to_template_context=True %}
      		{% widget_attribute "value" %}
      				run {{ loop.index }}
      		{% end_widget_attribute %}
      	{% end_widget_block %}

        {% for d in range(num) %}

	    {{ texter }}<br><br>

        {% endfor %}
      
      {% endfor %}

 

 This creates the text module in the outer loop then outputs the contents of value of the text module from each iteration of the outer loop, for each iteration of the inner loop.

 

The only problem is that my "texter" variable that I'm using to dynamically build the token for the text module value become a string and I am still looking for away to turn it into an expression. Currently it outputs the token as a string instead of processing to output the value of the modules. Maybe you can play off of this.

vin-aws
投稿者

Module unique_in_loop inside nested for loop

Essentially what I was making was a tabbed menu where each tab contains 3 more tabs. I ended up hard coding the 3 tabs for each tab.

0 いいね!
Jsum
キーアドバイザー

Module unique_in_loop inside nested for loop

@vin-aws,

 

You should consider using Hubspots design manager beta. The custom module's there have been updated to allow for repeater fields, and nested repeater fields. Much Much MUCH easier than hacking modules like you were trying.

0 いいね!
vin-aws
投稿者

Module unique_in_loop inside nested for loop

@Jsum we're not fully migrated over to the beta design manager yet. In any case, I've completed development of the module and works as expected. Thank you for your effort anyways.

0 いいね!