arguments to function calls and filters, or just to extend or include a
template).
-42 / 42.23:
- Integers and floating point numbers are created by just writing the
- number down. If a dot is present, the number is a float, otherwise an
+42:
+ Integers numbers are created by just writing the number down.
+ If a dot is present, the number will be considered a float, otherwise an
integer. Keep in mind that, in Python, ``42`` and ``42.0``
are different (``int`` and ``float``, respectively).
+42.23 / 42e2 / 42E2 / 1e0:
+ Floating point numbers are created by just writing the
+ number down. Floating points can be written using the dot as a decimal mark,
+ or they can be written in scientific notation in which
+ case you can use lower case 'e' or upper case 'E' to indicate the exponent
+ part.
+
['list', 'of', 'objects']:
Everything between two brackets is a list. Lists are useful for storing
sequential data to be iterated over. For example, you can easily
from jinja2.exceptions import TemplateSyntaxError
from jinja2.utils import LRUCache
+from ast import literal_eval # to support scientific notation
+
# cache for the lexers. Exists in order to be able to have multiple
# environments with the same lexer
_lexer_cache = LRUCache(50)
del jinja2._identifier
del _identifier
-float_re = re.compile(r'(?<!\.)\d+\.\d+')
+# Note: Float now supports 0 or 1 dots, and must thus be evaluated in the right
+# order so that pure integers are caught first. Floats can now be written with
+# scientific notation.
+float_re = re.compile(r'(?<!\.)\d+\.?\d+(?:e-?\d+)?', re.IGNORECASE)
+
newline_re = re.compile(r'(\r\n|\r|\n)')
# internal the tokens and keep references to them
elif token == 'integer':
value = int(value)
elif token == 'float':
- value = float(value)
+ value = literal_eval(value)
elif token == 'operator':
token = operators[value]
yield Token(lineno, token, value)
def test_float(self, env):
tmpl = env.from_string('{{ "42"|float }}|'
'{{ "ajsghasjgd"|float }}|'
+ '{{ "10e1"|float }}|'
+ '{{ "10.5e-10"|float }}|'
'{{ "32.32"|float }}')
out = tmpl.render()
- assert out == '42.0|0.0|32.32'
+ assert out == '42.0|0.0|100.0|1.05e-09|32.32'
def test_format(self, env):
tmpl = env.from_string('''{{ "%s|%s"|format("a", "b") }}''')