Created
February 13, 2012 21:59
-
-
Save gyndav/1820829 to your computer and use it in GitHub Desktop.
Using jinja2 within web.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import web | |
from jinja2 import Environment,FileSystemLoader | |
# Router | |
urls = ( | |
"/.*", "hello", | |
'/contact', 'rsvp' | |
) | |
app = web.application(urls, globals()) | |
# Define template rendering with jinja2 without overriding native render | |
def render_template(template_name, **context): | |
extensions = context.pop('extensions', []) | |
globals = context.pop('globals', {}) | |
jinja_env = Environment( | |
loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')), | |
extensions=extensions, | |
) | |
jinja_env.globals.update(globals) | |
return jinja_env.get_template(template_name).render(context) | |
# Controller | |
class hello: | |
def GET(self): | |
return render_template('index.html') | |
class rsvp: | |
def GET(self): | |
pass | |
if __name__ == "__main__": | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you exactly what I have been trying to do to avoid render.blah(foo=bar) 👍