Issue
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?
Solution
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()
Answered By - Adam Wagner
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.