1

I can't understand, why this code is not work:

{% set t_c = 'param_1' %}
                    <div class="col-sm-9">
                        <select id="category" name="category" class="form-control " required>
                            <option></option>
                            {% for c in categories %}
                                {% if c.id|string == org.category.id %}
                                    {% set t_c = 'param9' %}
                                    <option value="{{ c.id }}" selected>{{ c.name }} </option>
                                {% else %}
                                    <option value="{{ c.id }}">{{ c.name }} </option>
                                {% endif %}                          
                            {% endfor %}
                        </select>
                    </div>
  <input id="category_h" name="category_h" type="hidden" value="{{ t_c }}">

Why t_c in last line is 'param_1', when condition {% if c.id|string == org.category.id %} is true?

Thanks.

UPDATE

I have fast solution on JavaScript with jQuery+Select2 like:

var   category = $("#category"),
        category_h = $("#category_h");

 category.select2();
 category_h.val(category.find("option:selected").text());
Chebevara
  • 184
  • 2
  • 7

1 Answers1

1

t_c = 'param9' is local to the scope of the for loop

There are workarounds to extend beyond inner block scope

Community
  • 1
  • 1
jamylak
  • 128,818
  • 30
  • 231
  • 230