djblets.util.compat.django.template.loader¶
Compatibility functions for working with templates.
This module contains compatibility functions for working with templates. Django 1.8 introduced a new way of working with template backends, and with this came API changes. The functions in this file help to transition code over to the new function signatures without breaking older versions of Django.
-
render_to_string
(template_name, context=None, request=None, using=None)[source]¶ Render a template with a given name to a string.
This will invoke the original
render_to_string()
with the correct arguments for the active version of Django. It differs a bit from the modern version of the function in that it allows aContext
argument, which it will convert to the correct type.Parameters: - template_name (unicode) – The name of the template to render.
- context (object, optional) – The context data as a dictionary or as a
Context
. - request (django.http.HttpRequest, optional) – The HTTP request for the render. If specified, a
RequestContext
will be used. - using (unicode, optional) – The optional name of a template engine to use for the render. This is only supported on Django 1.8 and higher.
Returns: The resulting rendered content.
Return type:
-
render_template
(template, context={}, request=None)[source]¶ Render a loaded template to a string.
This will invoke the
render()
method on the template object, passing in the correct arguments for the active version of Django.If the template provided is a
django.template.Template
, then render calls are consistent across Django versions. However, if it’s a template returned from a template backend on Django 1.8 or higher, this will pass in the raw dictionary instead of aContext
object to render.Parameters: - template (object) – The template to render.
- context (dict, optional) – The context data as a dictionary.
- request (django.http.HttpRequest, optional) – The HTTP request for the render. If specified, a
RequestContext
will be used.
Returns: The resulting rendered content.
Return type: