-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
please add a "pyformat" filter #70
Comments
The {{ '%d' % x }}
{{ '%s:%d' % (x, y) }}
{{ '%(foo)s' % {'foo': 'bar'} }} Written that way, the code is less, simpler and more readable. Also note that using the modulo operator instead of the >>> print env.compile("{{ '%d'|format(x) }}", raw=True)
from __future__ import division
from jinja2.runtime import LoopContext, TemplateReference, Macro, Markup, TemplateRuntimeError, missing, concat, escape, markup_join, unicode_join, to_string, identity, TemplateNotFound
name = None
def root(context, environment=environment):
l_x = context.resolve('x')
t_1 = environment.filters['format']
if 0: yield None
yield to_string(t_1('%d', l_x))
blocks = {}
debug_info = '1=9' >>> print env.compile("{{ '%d' % x }}", raw=True)
from __future__ import division
from jinja2.runtime import LoopContext, TemplateReference, Macro, Markup, TemplateRuntimeError, missing, concat, escape, markup_join, unicode_join, to_string, identity, TemplateNotFound
name = None
def root(context, environment=environment):
l_x = context.resolve('x')
if 0: yield None
yield to_string(('%d' % l_x))
blocks = {}
debug_info = '1=8' Therefor I don't like the idea of adding another useless filter for formatting values, as that can also be accomplished the same, more elegant way by using the modulo operator. |
imagine the situation:
how would you do this, using the modulo operator? |
{{ '%0.2f' % vvv|float }} ... or if you prefer the {{ '%0.2f'|format(vvv|float) }} |
but this way the flow of information goes less linearly, instead of left to right (1, 2, 3) it goes middle to right to left (2, 3, 1). I understand your argument for speed, but readability is also worth considering... |
In my opinion the modulo operator variant is more readable than using the built-in {{ '%i points (%0.2f%%)' % (points, total / points * 100) }} Using {{ points|pyformat('i') + 'points (' + (total / points * 100)|pyformat('0.2f') + '%)' }} Do you still think that this it more readable? I agree that the built-in The problems with adding another filter for string formatting, that works like your
|
well, I would write it like this: ... {{ points|pyformat('i') }} points ({{ total / points * 100)|pyformat('0.2f')}}%) ... but I agree, in this case the single use of |
Here's a simple approach with a user-defined filter. If the overall filter behavior of this appeals to you, one of us could easily add to Jinja standard filters and make a pull request. Your thoughts on this? (1) example of a message to be interpolated -- a custom flask-security error message:
(2) custom filter defined in a flask app, for example:
(3) filter as used used in a Jinja template:
This allows string interpolation via a filter, given a dictionary to interpolate from -- exact analog of |
I think the |
the current implementation of filters allows us to pipe one value through filters and combine the effect of each step.
I see the "format" filter as an exception.
it filters a format according to values, instead of filtering a value according to a format.
I think the following filter would be more useful:
you would use it like this:
this follows the documentation of the old
format
filter, as I found it on http://wsgiarea.pocoo.org/jinja/docs/filters.html#formatThe text was updated successfully, but these errors were encountered: