whitebear
whitebear

Reputation: 12423

Use a variable in twig literal

Twig has date function such as

{% if today > date("+2days") %}

I want to use a variable in '+2days' for example

{{ x }} = 2

{% date('+xdays') %}

I can make it in PHP such as

date("+{$x} day");

But I use {} in twig, it shows error.

How can I slove this?

Upvotes: 0

Views: 1448

Answers (1)

edo.schatz
edo.schatz

Reputation: 99

Use set with concatenator in Twig like this

{% set x = 2 %}

{% set var = '+' ~ x ~ 'days' %}

{% date(var) %}

Upvotes: 3

Related Questions