Alternative Syntax — Jinja Documentation

# Jinja

## Alternative Syntax

Jinja allows some syntax customization for the block delimiters. Depending on the Jinja release more or less combinations are possible. The idea of an customizable syntax is that you can update existing templates from other template engines and programming languages easier and that you can replace the django like default delimiters which are not everybody's favorite.

## Configuring The Environment

For syntax configuration there are six arguments in the environment which are the first six for convenience. Thus those two snippets do the same:

    env = Environment(
         block_start_string='{%',
         block_end_string='%}',
         variable_start_string='{{',
         variable_end_string='}}',
         comment_start_string='{#',
         comment_end_string='#}',
    )

    env = Environment('{%', '%}', '{{', '}}', '{#', '#]')

### Ruby Like Syntax

Here an example configuration for Ruby-like syntax:

    env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>')

An example template then looks like this:

    <%# example eruby like configuration for Jinja %>
    <ul>
    <% for item in seq %>
      <li><%= item %></li>
    <% end‭for %>
    </ul>

### SGML Comment Syntax

Here an example configuration that uses SGML comments to hide the processing instructions. This can be useful if you work with an WYSIWYG designer:

    env = Environment('<!--', '-->', '${', '}', '<!--#', '-->')

    <!--# example SGML comment configuration for Jinja -->
    <ul>
    <!-- for item in seq -->
      <li>${item}</li>
    <!-- endfor -->
    </ul>

### Parenthesis Balancing

Starting with Jinja 1.1 it's possible to use the block delimiter as a token in the expression. That means that you can use small delimiters like single braces or parenthesis. So if you want to have your variables to look like `${expr}` and you still want to use dicts in such expressions you need Jinja 1.1 or higher.

## Block / Variable Tag Unification

If variable end and start tags are None or look the same as block tags and you're running Jinja 1.1 or later the parser will switch into the no_comment_block mode. In that mode it will try to match something as block first and continue treating it as variable block if there is no known directive for that name. Example:

    env = Environment('{', '}', None, None, '{*', '*}')

This now allows smarty like templates:

    {* example smarty-like configuration for Jinja *}
    {if something == 42}
        Something is the answer to all questions and stuff like that.
    {else}
        Something is {something}.
    {endif}

This feature however can cause strange looking templates because there is no visible difference between blocks and variables.