Added django.core.template_loader.render_to_string and django.core.extensions.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
This commit is contained in:
Adrian Holovaty 2005-09-22 14:57:05 +00:00
parent 17418cd982
commit 62f036d67d
2 changed files with 19 additions and 10 deletions

View File

@ -5,14 +5,10 @@ from django.core.template import Context
from django.conf.settings import DEBUG, INTERNAL_IPS
from django.utils.httpwrappers import HttpResponse
def load_and_render(template_name, dictionary=None, context_instance=None):
dictionary = dictionary or {}
t = template_loader.get_template(template_name)
if context_instance:
context_instance.update(dictionary)
else:
context_instance = Context(dictionary)
return HttpResponse(t.render(context_instance))
def render_to_response(*args, **kwargs):
return HttpResponse(template_loader.render_to_string(*args, **kwargs))
load_and_render = render_to_response # For backwards compatibility.
class DjangoContext(Context):
"""

View File

@ -19,6 +19,19 @@ def get_template_from_string(source):
"""
return template.Template(source)
def render_to_string(template_name, dictionary=None, context_instance=None):
"""
Loads the given template_name and renders it with the given dictionary as
context. Returns a string.
"""
dictionary = dictionary or {}
t = get_template(template_name)
if context_instance:
context_instance.update(dictionary)
else:
context_instance = template.Context(dictionary)
return t.render(context_instance)
def select_template(template_name_list):
"Given a list of template names, returns the first that can be loaded."
for template_name in template_name_list:
@ -119,8 +132,8 @@ def do_block(parser, token):
def do_extends(parser, token):
"""
Signal that this template extends a parent template.
This tag may be used in two ways: ``{% extends "base" %}`` (with quotes)
This tag may be used in two ways: ``{% extends "base" %}`` (with quotes)
uses the literal value "base" as the name of the parent template to extend,
or ``{% entends variable %}`` uses the value of ``variable`` as the name
of the parent template to extend.