Permalink
Browse files

Very basic template support - after umming and aahing for months, her…

…e's the simplest thing that could possibly work. See example_template.py for an example.
  • Loading branch information...
1 parent 158c512 commit 953eb33390972cbdd0ac0a52e3b23bfdd55e2cfe Simon Willison committed Jul 4, 2009
Showing with 44 additions and 1 deletion.
  1. +2 −1 djng/__init__.py
  2. +7 −0 djng/template/__init__.py
  3. +21 −0 djng/template/template_response.py
  4. +13 −0 example_template.py
  5. +1 −0 example_templates/example.html
View
@@ -12,4 +12,5 @@
from django import forms
from django.utils.html import escape
from django.utils.safestring import mark_safe
-#from template import TemplateResponse
+import template
+from template import TemplateResponse
@@ -0,0 +1,7 @@
+from template_response import TemplateResponse
+from django.conf import settings
+
+def configure(template_dirs):
+ if isinstance(template_dirs, basestring):
+ template_dirs = [template_dirs]
+ settings.TEMPLATE_DIRS = template_dirs
@@ -0,0 +1,21 @@
+from djng.response import Response
+from django.template import loader, RequestContext
+
+class TemplateResponse(Response):
+ def __init__(self, request, template, context = None):
+ self.context = context or {}
+ self.template = template
+ self.request = request
+ super(TemplateResponse, self).__init__()
+
+ def get_container(self):
+ return [
+ loader.get_template(self.template).render(
+ RequestContext(self.request, self.context)
+ )
+ ]
+
+ def set_container(self, *args):
+ pass # ignore
+
+ _container = property(get_container, set_container)
View
@@ -0,0 +1,13 @@
+import djng, os, datetime
+
+djng.template.configure(
+ os.path.join(os.path.dirname(__file__), 'example_templates')
+)
+
+def index(request):
+ return djng.TemplateResponse(request, 'example.html', {
+ 'time': str(datetime.datetime.now()),
+ })
+
+if __name__ == '__main__':
+ djng.serve(index, '0.0.0.0', 8888)
@@ -0,0 +1 @@
+Hello from djng! {{ time }}

0 comments on commit 953eb33

Please sign in to comment.