Question

Is it possible to use a variable from the parent's for loop within the child's if statement?

This is the example:

{% for ruleset in rulesets %}
  <div>{{ ruleset.0 }}</div>
  <ul>
    {% for rule in rules %}
      {% if rule.0 = {{ ruleset.0 }} %}
        <li>{{ rule.1 }}</li>
      {% else %}
        <!-- Nothing -->
      {% endif %}
    {% endfor %}
  </ul>
{% endfor %}

The error I am getting is: raise TemplateSyntaxError("Could not parse the remainder: '%s' from '%s'" % (token[upto:], token)) TemplateSyntaxError: Could not parse the remainder: '{{' from '{{'

Which I presume means that it couldn't understand {{ ruleset.0 }} within the if statement. Any suggestions as to how to resolve this?

Was it helpful?

Solution

Your syntax is incorrect: you can't use {{ ... }} inside a {% ... %} statement.

This should work, as the inner for loop should inherit the scope of the outer for loop:

{% for ruleset in rulesets %}
<div>{{ ruleset.0 }}</div>
<ul>
  {% for rule in rules %}
    # = is an assignment operator (which doesn't work in templates),
    # == is the equality operator, which you want to use.
    # alternatively you can use {% ifequal rule.0 ruleset.0 %}{% else %}{% endifequal %}
    {% if rule.0 == ruleset.0  %} 
      <li>{{ rule.1 }}</li>
    {% else %}
      <!-- Nothing -->
    {% endif %}
  {% endfor %}
</ul>
{% endfor %}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top