Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added django.core.template_loader.render_to_string and django.core.ex…

…tensions.render_to_response. django.core.extensions.load_and_render is deprecated in favor of render_to_response.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@664 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 62f036d67dacf8955946c4cccf7e2d30fa0616ea 1 parent 17418cd
Adrian Holovaty authored September 22, 2005
12  django/core/extensions.py
@@ -5,14 +5,10 @@
5 5
 from django.conf.settings import DEBUG, INTERNAL_IPS
6 6
 from django.utils.httpwrappers import HttpResponse
7 7
 
8  
-def load_and_render(template_name, dictionary=None, context_instance=None):
9  
-    dictionary = dictionary or {}
10  
-    t = template_loader.get_template(template_name)
11  
-    if context_instance:
12  
-        context_instance.update(dictionary)
13  
-    else:
14  
-        context_instance = Context(dictionary)
15  
-    return HttpResponse(t.render(context_instance))
  8
+def render_to_response(*args, **kwargs):
  9
+    return HttpResponse(template_loader.render_to_string(*args, **kwargs))
  10
+
  11
+load_and_render = render_to_response # For backwards compatibility.
16 12
 
17 13
 class DjangoContext(Context):
18 14
     """
17  django/core/template_loader.py
@@ -19,6 +19,19 @@ def get_template_from_string(source):
19 19
     """
20 20
     return template.Template(source)
21 21
 
  22
+def render_to_string(template_name, dictionary=None, context_instance=None):
  23
+    """
  24
+    Loads the given template_name and renders it with the given dictionary as
  25
+    context. Returns a string.
  26
+    """
  27
+    dictionary = dictionary or {}
  28
+    t = get_template(template_name)
  29
+    if context_instance:
  30
+        context_instance.update(dictionary)
  31
+    else:
  32
+        context_instance = template.Context(dictionary)
  33
+    return t.render(context_instance)
  34
+
22 35
 def select_template(template_name_list):
23 36
     "Given a list of template names, returns the first that can be loaded."
24 37
     for template_name in template_name_list:
@@ -119,8 +132,8 @@ def do_block(parser, token):
119 132
 def do_extends(parser, token):
120 133
     """
121 134
     Signal that this template extends a parent template.
122  
-    
123  
-    This tag may be used in two ways: ``{% extends "base" %}`` (with quotes) 
  135
+
  136
+    This tag may be used in two ways: ``{% extends "base" %}`` (with quotes)
124 137
     uses the literal value "base" as the name of the parent template to extend,
125 138
     or ``{% entends variable %}`` uses the value of ``variable`` as the name
126 139
     of the parent template to extend.

0 notes on commit 62f036d

Please sign in to comment.
Something went wrong with that request. Please try again.