python-2.7cheetah

Getting python Cheetah to print backslash


I am having trouble getting the Python Cheetah to print a backslash for me.

Cheetah version 2.4.4

from Cheetah.Template import Template

my_template = """
Stuff $var1\\$var2
"""

t = Template(source=my_template, searchList = [{"var1" : """\\x\y\z""", "var2" : "some"}])
print str(t)

I expected to see the output

Stuff \\x\y\z\some

But I get

Stuff \x\y\z$var2

What should the template be in this case?


Solution

  • An easy way to get this working is to use the cheetah <%= => syntax

    my_template = """
    Stuff $var1<%= '\\\\'%>$var2
    """