Search code examples
pythonbottle

Python Bottle template engine does not allow period


It looks like the Bottle template engine does not periods in it's template variables. The following code:

from bottle import template
x = 'blah {{blah.blah}} blah'
d = {'blah.blah': 'doodah'}
template(x, **d)

Gives me this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "E:\WPy-3662\python-3.6.6.amd64\lib\site-packages\bottle.py", line 3622, in template
    return TEMPLATES[tplid].render(kwargs)
  File "E:\WPy-3662\python-3.6.6.amd64\lib\site-packages\bottle.py", line 3411, in render
    self.execute(stdout, env)
  File "E:\WPy-3662\python-3.6.6.amd64\lib\site-packages\bottle.py", line 3398, in execute
    eval(self.co, env)
  File "<string>", line 1, in <module>
NameError: name 'blah' is not defined

I need periods. I'm keeping my templates in a multi-line string array and then passing an array of dictionaries to the Bottle template engine.

How can I get around this?


Solution

  • So the problem is not with Bottle, but in how python parses the variables during eval.

    The template string blah.blah When run through eval creates a variable name blah.blah, which turns into a two part notation.

    blah.blah = 'doodah'
    

    can't work. Though it does work in a dictionary since it is a string name. If you replace the dot with an underline, it will work, because an underline is a valid variable name.