how to do a while ( x < y ) in jinja2

  • Last Update :
  • Techknowledgy :

I think your closest alternative with Jinja2 would be to use a for with range:

  {
     % range number from 3 to 6 %
  } {
     {
        number
     }
  }
  (...) {
     % endrange %
  }

Suggestion : 2

A template contains variables and/or expressions, which get replaced with values when a template is rendered; and tags, which control the logic of the template. The template syntax is heavily inspired by Django and Python.,By default, included templates are passed the current context and imported templates are not. The reason for this is that imports, unlike includes, are cached; as imports are often used just as a module that holds macros.,But this behavior can depend on the application embedding Jinja. Note that since the child template doesn’t define the footer block, the value from the parent template is used instead.,You can also provide a list of templates that are checked for existence before inclusion. The first template that exists will be included. If ignore missing is given, it will fall back to rendering nothing if none of the templates exist, otherwise it will raise an exception.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>My Webpage</title>
</head>
<body>
    <ul id="navigation">
    {% for item in navigation %}
        <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
    {% endfor %}
    </ul>

    <h1>My Webpage</h1>
    {{ a_variable }}

    {# a comment #}
</body>
</html>
{
   {
      foo.bar
   }
} {
   {
      foo['bar']
   }
}
{
   %
   if loop.index is divisibleby 3 %
} {
   %
   if loop.index is divisibleby(3) %
}
{
   # note: commented - out template because we no longer use this {
         %
         for user in users %
      }
      ...{
         % endfor %
      }
   #
}
<div>
    {% if True %}
        yay
    {% endif %}
</div>
<div>

   yay

</div>

Suggestion : 3

Another basic feature of Jinja is variables. Sometimes, you need the computer to remember some values while rendering your template. In order to do so, you can create a variable, that will store the value for you. A variable always has a name, by which it can be referred to during the rendering, and it also has a type. However, you do not need to worry about types, for now. Jinja automatically sets them up for you when you initialize a variable. ,Make sure you always initialize your variables before using them, otherwise, the parser will not be able to understand what you are referring to by the variable name.,Jinja also inherits the comparison operators from python. They are essential when implementing control flow, which will be covered in a later article. The official documentation for comparison expressions can be found in Template designer documentation - Comparisons.,Make sure that you give your variables relevant names. It will make your code more readable and it will also help you eliminate errors. For instance, you can call a variable storing a number x, or a variable storing some customer's name, customerName.

{
   % set favouriteAnimal = "duck" %
} {
   {
      favouriteAnimal
   }
}
Output: duck

Suggestion : 4

Last Updated : 11 Jan, 2022

Syntax
{
   %
   for i in list %
} {
   % endfor %
}

If you need to loop over a list of lists, you can unpack the values in each sublist into individual variables. For example, if your context contains a list of (x, y) coordinates called points, you could use the following to output the list of points:

{
   %
   for x, y in points %
}
There is a point at {
   {
      x
   }
}, {
   {
      y
   }
} {
   % endfor %
}

This can also be useful if you need to access the items in a dictionary. For example, if your context contained a dictionary data, the following would display the keys and values of the dictionary:

{
   %
   for key, value in data.items %
} {
   {
      key
   }
}: {
   {
      value
   }
} {
   % endfor %
}

If you need to loop over a list of lists, you can unpack the values in each sublist into individual variables. For example, if your context contains a list of (x, y) coordinates called points, you could use the following to output the list of points:

{
   %
   for x, y in points %
}
There is a point at {
   {
      x
   }
}, {
   {
      y
   }
} {
   % endfor %
}

This can also be useful if you need to access the items in a dictionary. For example, if your context contained a dictionary data, the following would display the keys and values of the dictionary:

{
   %
   for key, value in data.items %
} {
   {
      key
   }
}: {
   {
      value
   }
} {
   % endfor %
}

Suggestion : 5

On certain occasions you may need to nest multiple {% for %} statements and access parent loop items. In Django templates, this is easy because there's a variable for just this purpose. However, Jinja templates don't have this variable as you can see in table 4-1. A solution in Jinja templates is to define a reference variable with {% set %} before entering the child loop to gain access to the parent loop, as illustrated in the following snippet,{% block %}.- The {% block %} statement is used to define page sections that can be overridden on different Jinja templates. See the previous section on creating reusable Jinja templates for detailed examples of this statement.,We respect your privacy, however, the content you're reading wouldn't be freely available without revenue from advertisers.,{% extends %}.- The {% extends %} statement is used to reuse the layout of another Jinja template. See the previous section on creating reusable Jinja templates for detailed examples of this statement.

 {% for element in list %}
      {% if loop.changed(element) %}
            <li>{{element}} changed!
      {% else %}
            <li>{{element}} did not change
      {% endif %}
 {% endfor %}

On certain occasions you may need to nest multiple {% for %} statements and access parent loop items. In Django templates, this is easy because there's a variable for just this purpose. However, Jinja templates don't have this variable as you can see in table 4-1. A solution in Jinja templates is to define a reference variable with {% set %} before entering the child loop to gain access to the parent loop, as illustrated in the following snippet

<ul>
   {% for chapter in chapters %}
    {% set chapterloop = loop %}
      {% for section in chapter %}
       <li> {{chapterloop.index }}.{{ loop.index }}">{{ section }}</li>
      {% endfor %}
   {% endfor %}
</ul>

Another nested loop feature in Jinja templates is cycle, which does not exist in Django templates (as a variable at least, it does exist as a tag). The primary use of cycle is to define CSS classes so each iteration receives a different CSS class and upon rendering each iteration is displayed in a different color. The following snippet illustrates the use of the cycle variable.

{% for drink in drinks %} 
   <li class="{{ loop.cycle('odd','even') }}">{{ drink.name }}</li>
{% endfor %}

Suggestion : 6

If you need to loop over a list of lists, you can unpack the values in each sublist into individual variables. For example, if your context contains a list of (x,y) coordinates called points, you could use the following to output the list of points:,Defines a block that can be overridden by child templates. See Template inheritance for more information.,This can also be useful if you need to access the items in a dictionary. For example, if your context contained a dictionary data, the following would display the keys and values of the dictionary:,If you are chaining filters, a filter applied after safe can make the contents unsafe again. For example, the following code prints the variable as is, unescaped:

{
   % autoescape on %
} {
   {
      body
   }
} {
   % endautoescape %
}
<p>Rendered text with {{ pub_date|date:"c" }}</p>
{% comment "Optional note" %}
    <p>Commented out text with {{ create_date|date:"c" }}</p>
{% endcomment %}
{% for o in some_list %}
    <tr class="{% cycle 'row1' 'row2' %}">
        ...
    </tr>
{% endfor %}
{% for o in some_list %}
    <tr class="{% cycle rowvalue1 rowvalue2 %}">
        ...
    </tr>
{% endfor %}
{% for o in some_list %}
    <tr class="{% autoescape off %}{% cycle rowvalue1 rowvalue2 %}{% endautoescape %}">
        ...
    </tr>
{% endfor %}
{% for o in some_list %}
    <tr class="{% cycle 'row1' rowvalue2 'row3' %}">
        ...
    </tr>
{% endfor %}