pallets / jinja

A very fast and expressive template engine.

Home Page:https://jinja.palletsprojects.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TemplateSyntaxError: unexpected char '\\'

drdv opened this issue · comments

Let the content of template.txt.j2 be

{{ '1' }} "2"

when I execute

import jinja2
from jinja2 import meta

env = jinja2.Environment(
    loader=jinja2.FileSystemLoader("."),
    keep_trailing_newline=True,
    undefined=jinja2.StrictUndefined,
)

template_source = env.loader.get_source(env, "template.txt.j2")
parsed_content = env.parse(template_source)  # causes the problem

I get the following exception

TemplateSyntaxError: unexpected char '\\' at 5

If I change the template to

{{ "1" }} "2"

things work as expected. Note that the output of env.get_template("template.txt.j2").render() for both templates is

'1 "2"\n'

Based on the docs (https://jinja.palletsprojects.com/en/3.1.x/templates/#escaping) I expect that syntax like {{ '{{' }} should work.

  • Python version: 3.10.2
  • Jinja version: 3.1.2

This is not a bug. The parse function should be called using env.parse(*template_source). I was trying to use the example from https://stackoverflow.com/a/8284419 and I didn't check the function signature.