Policies

Starting with Jinja 2.9 policies can be configured on the environmentwhich can slightly influence how filters and other template constructsbehave. They can be configured with thepolicies attribute.

Example:

  1. env.policies['urlize.rel'] = 'nofollow noopener'
  • compiler.ascii_str:
  • This boolean controls on Python 2 if Jinja2 should store ASCII onlyliterals as bytestring instead of unicode strings. This used to bealways enabled for Jinja versions below 2.9 and now can be changed.Traditionally it was done this way since some APIs in Python 2 failedbadly for unicode strings (for instance the datetime strftime API).Now however sometimes the inverse is true (for instance str.format).If this is set to False then all strings are stored as unicodeinternally.

  • truncate.leeway:

  • Configures the leeway default for the truncate filter. Leeway asintroduced in 2.9 but to restore compatibility with older templatesit can be configured to 0 to get the old behavior back. The defaultis 5.

  • urlize.rel:

  • A string that defines the items for the rel attribute of generatedlinks with the urlize filter. These items are always added. Thedefault is noopener.

  • urlize.target:

  • The default target that is issued for links from the urlize filterif no other target is defined by the call explicitly.

  • json.dumps_function:

  • If this is set to a value other than None then the tojson filterwill dump with this function instead of the default one. Note thatthis function should accept arbitrary extra arguments which might bepassed in the future from the filter. Currently the only argumentthat might be passed is indent. The default dump function isjson.dumps.

  • json.dumps_kwargs:

  • Keyword arguments to be passed to the dump function. The default is{'sort_keys': True}.
  • ext.i18n.trimmed:
  • If this is set to True, {% trans %} blocks of thei18n Extension will always unify linebreaks and surroundingwhitespace as if the trimmed modifier was used.