3
votes

I'm trying to learn some Bottle basics and follow the tutorial on the bottlepy.org pages. First I'll say: running on Ubuntu (12.04 I think?). I installed bottle via sudo easy_install bottle which installed it only into my python2.7 dist-packages. I've read somewhere that bottle.py is intentionally dependency-less, and that copying bottle.py into an available directory to get it to work in python3 (I'm trying to use python3.2) is reasonable.

MWE:

In helloworld.wsgi I have:

#!/usr/bin/python3
from bottle import debug, route, run, template, view

@route("/hello")
@route("/hello/<name>")
@view("base")
def hello(name=None):
    return dict(name=name)

debug(True)
run(host="localhost", port=8080)

And in views/base.tpl I have:

<% 
  if name is not None:
      name = name.title().strip()
  else:
      name = "World"
%>
<p>Hello {{name}}!</p>

But when I try to navigate to a site (either localhost:8080/hello or localhost:8080/hello/dude ) I'm getting an error:

SyntaxError: invalid syntax

referring to the '>' that closes the '%>' (line 6).

I'm not sure why I'm getting this error - I pretty much copied the examples from the website verbatim, and don't know how else to enclose python code blocks in template text (I don't think using % at the beginning of every code line is as reasonable a way to do it).

Any thoughts or ideas? Thanks

1

1 Answers

4
votes

I think you're looking at the documentation for a different version (0.13-dev) of Bottle from the one you're using (0.11.6).

The current "stable" version of Bottle is 0.11. It looks like the <% ... %> feature of SimpleTemplate was added some time between 0.12 and 0.13-dev, the current "dev" branch.

Here are the relevant 0.11 docs.


You can work around this in a few ways:

  1. You could move to a newer, unreleased version of Bottle. Risky.

  2. You could use the 0.11 mechanism for embedding Python code, % .. %end. But this is clearly deprecated, not to mention annoying for longer blocks of code.

  3. You could use a more robust templating language; Bottle integrates nicely with several. I chose Jinja2 and I recommend it. It's nearly as simple as Bottle's built-in templates but much more flexible. Here is someone else's similarly positive experience with Bottle + Jinja2.