0

I'm looking for a way to get some id like :

first_1 / first_2 / etc etc

But currently i'm doing wrong

{% set nb = 0 %}
{% for resultat in resultats %}
    {% if resultat.first == 1 %}
        {% set nb=nb+1 %}
        <tr id="first_"{{ nb }}>

This is giving id="first_" and an other param "1"=""

Thanks for your help

JGrinon
  • 1,453
  • 1
  • 14
  • 36
  • 1
    A little bit of research would have helped you : [how to concatenate in twig](https://stackoverflow.com/questions/7704253/how-to-concatenate-strings-in-twig) – t-n-y Aug 09 '17 at 08:28

3 Answers3

2

You have:

  1. loop variables
  2. String Interpolation
{{ "first_#{loop.index0}" }}

you don't need to manually create the incrementer.

Further info: How to concatenate strings in twig

Hitmands
  • 13,491
  • 4
  • 34
  • 69
1

You can concat string:

{% set nb = 0 %}

<tr id="first_{{ nb }}">
JGrinon
  • 1,453
  • 1
  • 14
  • 36
0

You can concat string in Twig with "~" (same as "." in php and "+" in javascript)

    <tr id="{{ 'first_' ~ nb }}">
Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36