Skip to content

Instantly share code, notes, and snippets.

@eliotk
Created November 19, 2011 23:00
Show Gist options
  • Save eliotk/1379496 to your computer and use it in GitHub Desktop.
Save eliotk/1379496 to your computer and use it in GitHub Desktop.
A Python function to render a django view file with optional template dict of parameters passed in. This output could then be passed to a more general layout file using template again. Based on Usage in Google App Engine
import os
from django.template import Template, Context
from google.appengine.ext.webapp import template
def render_view(view_name, template_dict = {}):
path = os.path.join(os.path.dirname(__file__), view_name)
f = open(path, 'r')
view_str = f.read()
t = Template(view_str)
return t.render(Context(template_dict))
# usage in webapp handler could look like:
class MainHandler(webapp.RequestHandler):
def get(self):
layout_values = {
'view': render_view('index.html')
}
# template.render(path, template_values);
path = os.path.join(os.path.dirname(__file__), 'layout.html')
self.response.out.write(template.render(path, layout_values))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment