blob: 0f7242f79f495fa387999e1f23afa4911323b1eb [file] [log] [blame]
==================
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:
.. sourcecode:: python
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:
.. sourcecode:: python
env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>')
An example template then looks like this:
.. sourcecode:: rhtml
<%# 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:
.. sourcecode:: python
env = Environment('<!--', '-->', '${', '}', '<!--#', '-->')
.. sourcecode:: html
<!--# 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:
.. sourcecode:: python
env = Environment('{', '}', None, None, '{*', '*}')
This now allows smarty like templates:
.. sourcecode:: smarty
{* 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.