Looking to eliminate a for loop in my code (how does acessing option.values work?)

jassoul
New Member
5 0 0

Hello everyone,

I'm trying to do a modification on my theme in the product-form file. 

 

Basically I need to split this for loop in order to add conditional logic to each part individually but I dont understand how the for loop passes the information 

 

Original code:

 

 

              {%- for value in option.values -%}
                <li class="HorizontalList__Item">
                  <input id="option-{{ section.id }}-{{ forloop.parentloop.index0 }}-{{ forloop.index0 }}" class="SizeSwatch__Radio" type="radio" name="option-{{ forloop.parentloop.index0 }}" value="{{ value | escape }}" {% if value == option.selected_value %}checked="checked"{% endif %} data-option-position="{{ option.position }}">
                  <label for="option-{{ section.id }}-{{ forloop.parentloop.index0 }}-{{ forloop.index0 }}" class="SizeSwatch">{{ value }}</label>
                </li>
              {%- endfor -%}

Mockup of end result wanted:

THIS WOULD BE OPTION 1
                <li class="HorizontalList__Item">
                  <input id="option-{{ section.id }}-{{ forloop.parentloop.index0 }}-{{ forloop.index0 }}" class="SizeSwatch__Radio" type="radio" name="option-{{ forloop.parentloop.index0 }}" value="{{ value | escape }}" {% if value == option.selected_value %}checked="checked"{% endif %} data-option-position="{{ option.position }}">
                  <label for="option-{{ section.id }}-{{ forloop.parentloop.index0 }}-{{ forloop.index0 }}" class="SizeSwatch">{{ value }}</label>
                </li>

THIS WOULD BE OPTION 2
                <li class="HorizontalList__Item">
                  <input id="option-{{ section.id }}-{{ forloop.parentloop.index0 }}-{{ forloop.index0 }}" class="SizeSwatch__Radio" type="radio" name="option-{{ forloop.parentloop.index0 }}" value="{{ value | escape }}" {% if value == option.selected_value %}checked="checked"{% endif %} data-option-position="{{ option.position }}">
                  <label for="option-{{ section.id }}-{{ forloop.parentloop.index0 }}-{{ forloop.index0 }}" class="SizeSwatch">{{ value }}</label>
                </li>

 

 So in my limited understanding of what is happening here, I assume that "value" stores the option details and passes it to the list. How can I pass that information directly to the list and avoid the loop?

 

Thank you very much for the help

0 Likes