Search code examples
pythonjinja2

jinja2 variables naming - Are variables naming restrictions the same as for Python variables?


I did't find it written explicitly in the docs.

Are the naming rules the same as with Python variables?

(eg: {{ a_variable'like_that' }} doesn't work for example)


Solution

  • The complete and updated answer can be found at the following link:

    https://jinja.palletsprojects.com/en/3.1.x/api/#notes-on-identifiers

    Jinja uses Python naming rules. Valid identifiers can be any combination of characters accepted by Python.

    Filters and tests are looked up in separate namespaces and have slightly modified identifier syntax. Filters and tests may contain dots to group filters and tests by topic. For example it’s perfectly valid to add a function into the filter dict and call it to.str. The regular expression for filter and test identifiers is [a-zA-Z_][a-zA-Z0-9_](.[a-zA-Z_][a-zA-Z0-9_])*.