3

I can't seem to find any information about using the cheetah templating engine with flask. Can anyone point me to something that google can't find, or show me how to use cheetah templates in a simple flask app?

Thanks very much in advance.

Hoopes
  • 3,077
  • 4
  • 31
  • 50

1 Answers1

5

I am no Cheetah or Flask expert, but I don't think you need any special support to make it work. Looking at the examples for both, I came up with this (and it seems to work fine for me).

from flask import Flask
from Cheetah.Template import Template


mainTemplate = """
<html>
    <head><title>$title</title></head>
    <body><h1>$title</h1></body>
</html>"""


app = Flask(__name__)


@app.route('/')
def main_route():
    return render(mainTemplate, {'title': 'Welcome to "/"!'})


def render(template, context):
    """Helper function to make template rendering less painful."""
    return str(Template(template, namespaces=[context]))


if __name__ == "__main__":
    app.run()
Adam Wagner
  • 13,492
  • 6
  • 50
  • 64
  • Yep, you are right. Coming from some other frameworks, I thought there would be way more stuff involved (setting up some auto-render bit, etc.) This was very simple, and works great. Thanks a lot. – Hoopes Dec 04 '11 at 15:53