From 62f036d67dacf8955946c4cccf7e2d30fa0616ea Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Thu, 22 Sep 2005 14:57:05 +0000 Subject: [PATCH] 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 --- django/core/extensions.py | 12 ++++-------- django/core/template_loader.py | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/django/core/extensions.py b/django/core/extensions.py index 17e1c76734..6cbd1ebe28 100644 --- a/django/core/extensions.py +++ b/django/core/extensions.py @@ -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): """ diff --git a/django/core/template_loader.py b/django/core/template_loader.py index 2e2a098a86..c2b41f3743 100644 --- a/django/core/template_loader.py +++ b/django/core/template_loader.py @@ -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.