mitsuhiko / minijinja

MiniJinja is a powerful but minimal dependency template engine for Rust compatible with Jinja/Jinja2
https://docs.rs/minijinja/
Apache License 2.0
1.67k stars 101 forks source link

Proper way to do string formatting #427

Open rdbo opened 8 months ago

rdbo commented 8 months ago

I tried the following ways to format a float to have 2 decimal place precision, but neither worked:

{{ "%.2f" % price }}

Result: invalid operation: tried to use % operator on unsupported types string and number

{{ "%.2f"|format(price) }}

Result: unknown filter: filter format is unknown

Is it possible to format strings like this without creating a custom filter? If so, what is the proper way to do it?

mitsuhiko commented 8 months ago

Today a custom filter is the only thing you can do. I would love to have a formatting filter in minijinja-contrib but it's quite a bit of effort to create and I did not have the time yet. Happy to accept contributions.