jinja icon indicating copy to clipboard operation
jinja copied to clipboard

Make format work with map (useful for dbt)

Open TomAtGithub opened this issue 6 months ago • 0 comments

Hey, I am using jinja with dbt and would like to prefix a list of columns in a functional way. I am trying

{% set columns = ["title", "name"] | map("format", "prefix_%s") | join(",") %}

Unfortunately this does not work because format is not designed to be used with map. Map passes the variables in the "wrong" order:

filters.py line 1738

def func(item: t.Any) -> t.Any:
    return context.environment.call_filter(
        name, item, args, kwargs, context=context
    )

format uses this syntax:

{{ item % args }}
# Example
{{ "Hello, %s!" % name }}

but to work with map it should be something like this:

{{ args % item }}

It would be nice to have it, even though its just syntactic sugar

TomAtGithub avatar Jun 17 '25 08:06 TomAtGithub