HubSpot / jinjava

Jinja template engine for Java
Apache License 2.0
705 stars 169 forks source link

Cannot modify individual list or dict item using set #374

Open TheWebTech opened 5 years ago

TheWebTech commented 5 years ago

Code explains better than words could.

{% set items = ["item1","item2"] %}
{% set items[0] = "new value" %}
expected behavior: {{items}} = ["new value","item2"]
actual behavior: {{items}} = ["item1","item2"]

We have list functions, but none seem to recreate the same sort of simple functionality that many programming languages support.

jasmith-hs commented 3 years ago

One possible workaround for developers would be to do {% set items = ["new value"] + items[1:] %}

mattcoley commented 3 years ago

We could support setItem on Pylist. So this would look like {% do items.setItem(0, "new value") %}. There isn't supported directly in Jinja2 though.

jasmith-hs commented 3 years ago

{% do items.set(0, "new value") %} should work actually