Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to apply "format" on a list of strings? #545

Closed
htgoebel opened this issue Feb 16, 2016 · 4 comments
Closed

How to apply "format" on a list of strings? #545

htgoebel opened this issue Feb 16, 2016 · 4 comments

Comments

@htgoebel
Copy link

I try to do something like in Python

("db.%s.dump" % n for n in names)

I tried

{{ databases | map("format", "db.%s.dump") }}
{{ "db.%s.dump" | map("format", databases) }}

and a lot more. All I tried failed.

The documentation is of no help :-(

@rockwelln
Copy link

why not {% for n in names %} {{ "db.%s.dump"|format(n) }} {% endfor %}?

@krzysztof-magosa
Copy link

@NormanDenayer it's fine unless you want to pass it to another filter in map().

@krzysztof-magosa
Copy link

Personally I ended with writing own plugin:

from jinja2.utils import soft_unicode

def format_string(text, fmt, *kw):
    return soft_unicode(fmt).format(text, *kw)

class FilterModule(object):
    def filters(self):
        return dict(format_string=format_string)

Usage:

list_of_names | map('format_string', 'hello {}!')

@davidism
Copy link
Member

davidism commented Nov 4, 2019

See #906 (comment) for why I'm declining this. It also contains a filter that could accomplish this, or you can write a more specific filter as shown above.

@davidism davidism closed this as completed Nov 4, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants