Jinja Delimiters and Configuration
n
Jinja templates use specific characters, known as delimiters, to distinguish between static content (plain text) and dynamic template code. These delimiters act like signals, telling the Jinja engine where to find logic, variables, and comments. The default configuration is designed to be clear and readable, but an application developer can change these delimiters to suit their specific needs, for example, changing {% foo %} to <% foo %> if another templating engine is using the default Jinja syntax.
nn
Default Jinja Delimiters
n
The default delimiters are a key part of Jinja’s syntax. They are configured as follows:
nn
- n
{% ... %}for Statements: This delimiter is used for control flow statements, such as for loops ({% for ... %}), conditional statements ({% if ... %}), and macro definitions ({% macro ... %}). This is where the template’s logic resides.{{ ... }}for Expressions: This delimiter is used for expressions that should be printed to the template’s output. Any variable or expression enclosed in these double curly braces will be evaluated and its result inserted into the final document.{# ... #}for Comments: This delimiter is used for comments that are not included in the final template output. Comments are useful for documenting your template logic and are a key part of writing maintainable code.
n
n
n
n
nn
Customizing Delimiters and Line Statements
n
While the default delimiters are standard, Jinja allows for a high degree of customization. When creating the template environment in your application, you can override the default settings to use different characters. This is done by setting properties like block_start_string, block_end_string, variable_start_string, and so on.
n
Additionally, Jinja supports Line Statements and Comments, which are useful for scripting-style templates. These don’t have default prefix characters, but you can enable them by setting line_statement_prefix and line_comment_prefix when creating the Environment object. For example, you could set the line statement prefix to %, allowing you to write a statement like % for user in users:.
nn
n
