Jinja2Cpp icon indicating copy to clipboard operation
Jinja2Cpp copied to clipboard

Add 'flip' filter

Open flexferrum opened this issue 7 years ago • 2 comments

According to this pull request: https://github.com/pallets/jinja/pull/906

Applies a filter with arguments flipped in reverse order. This becomes useful with filters like map where you can't easily re-order the arguments yourself. Example 1: Say you have a list of names and you want to prefix them all with "Name: " you could do it like this: .. sourcecode:: jinja {{ ["Carl", "Jane"] | map("flip", "format", "Name: %s") | join(",") }} This would give you back: Name: Carl, Name: Jane Example 2: Say you have a User object like below: .. sourcecode:: python class User(object): def init(self, name, surname, age): self.name = name self.surname = surname self.age = age user = User('john', 'doe', 31) ... and you want to extract a couple of attribute values from it as a list and process that. This is one way to do that: .. sourcecode:: jinja {{ ['surname', 'name'] | map("flip", "attr", user) | map("capitalize") | join(", ") }} This would give you back Doe, John

flexferrum avatar Oct 05 '18 12:10 flexferrum

Note that it's unsure if this will get merged upstream. :)

mattiasb avatar Oct 10 '18 13:10 mattiasb

Thanks. :) In wrost case I hide it under the compatibility mode flag. :)

flexferrum avatar Oct 10 '18 14:10 flexferrum