Replace integer field value with stars in twig template I have a Drupal 8 site with a node type exposing a "difficulty level" field which is an integer choosen from a list (1 to 5).
Current onsite display is : "difficulty: 3". I would like to replace the value "3" by three black stars and two white ones to picture "3/5" (done via CSS background images).

In field--field-difficulty.html.twig I have replaced item.content by:

{% for i in range(1, 5) %}
  {% if (loop.index &lt;= item.content) %}
    &lt;span class="active"&gt;*&lt;/span&gt;
  {% else %}
    &lt;span class="active"&gt; &lt;/span&gt;
  {% endif %}
{% endfor %}


But this keep giving me five * displayed, just like if the {% if %} statement was always true.
Can someone give me a hint on this please ?
For the field template there are two approaches to get the integer number from a field:

Node object

{% set difficulty = element['#object'].field_difficulty.value %}
{% for i in range(1, 5) %}
  {% if i &lt;= difficulty %}


Find the rendered number

You can try to find the number in #markup:

{% if loop.index &lt;= item.content['#markup'] %}


The node object is the better approach, because the way how the number is rendered can change when updating the code or changing the configuration.