user8238012
user8238012

Reputation:

Twig & Html concatenation

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

Upvotes: 0

Views: 1823

Answers (3)

Micka&#235;l Leger
Micka&#235;l Leger

Reputation: 3440

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

    <tr id="{{ 'first_' ~ nb }}">

Upvotes: 0

Hitmands
Hitmands

Reputation: 14179

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

Upvotes: 2

JGrinon
JGrinon

Reputation: 1453

You can concat string:

{% set nb = 0 %}

<tr id="first_{{ nb }}">

Upvotes: 1

Related Questions