Jinja: Incorrect behavior of truncate filter

Created on 3 Apr 2017  ·  4Comments  ·  Source: pallets/jinja

Code:

from jinja2 import Template

print(Template('{{ "2016.10.26_cc37528" | truncate(11, True) }}').render())
print(Template('{{ "2016.10.26_cc37528" | truncate(12, True) }}').render())
print(Template('{{ "2016.10.26_cc37528" | truncate(13, True) }}').render())
print(Template('{{ "2016.10.26_cc37528" | truncate(14, True) }}').render())

Expected Behavior

2016.10....
2016.10.2...
2016.10.26...
2016.10.26_...

Actual Behavior

2016.10....
2016.10.2...
2016.10.26_cc37528
2016.10.26_cc37528

Template Code

{{ "2016.10.26_cc37528" | truncate(13, True) }}

Your Environment

  • Python version: 3.6.1
  • Jinja version: 2.9.5

Most helpful comment

It is. Setting leeway to 0 fixes the issue. I'd say it's POLA violation when using short strings.

All 4 comments

Check whether this is caused by the leeway argument. The default value for it changed recently.

https://github.com/pallets/jinja/blob/master/jinja2/filters.py#L467

It is. Setting leeway to 0 fixes the issue. I'd say it's POLA violation when using short strings.

Yeah I agree @AMDmi3 . We just hit this issue as well.

The problem is that Jinja has an opinionated implementation which is incompatible with usage as a general purpose templating engine.

I also found this problem and spend several hours finding the bug. Clearly new options should be backwards compatible and the default should be leeway = 0.
Whoever starts using the new option leeway they will know it exists and will use the value they please. Those who dont know about the new parameter wont need to change their code...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mitsuhiko picture mitsuhiko  ·  3Comments

priestc picture priestc  ·  5Comments

htgoebel picture htgoebel  ·  4Comments

Xion picture Xion  ·  5Comments

DriverX picture DriverX  ·  4Comments