Source code for superdjango.html.shortcuts

"""
Shortcuts, especially for working with Django templates in an ad-hoc manner.

"""

# Imports

from django.template import Context, loader, Template
from django.template.exceptions import TemplateDoesNotExist

# Exports

__all__ = (
    "parse_string",
    "parse_template",
    "template_exists",
)

# Functions


[docs]def parse_string(string, context): """Parse a string as a Django template. :param string: The name of the template. :type string: str :param context: Context variables. :type context: dict :rtype: str """ template = Template(string) return template.render(Context(context))
[docs]def parse_template(template, context): """Ad hoc means of parsing a template using Django's built-in loader. :param template: The name of the template. :type template: str || unicode :param context: Context variables. :type context: dict :rtype: str """ return loader.render_to_string(template, context)
[docs]def template_exists(name): """Indicates whether the given template exists. :param name: The name (path) of the template to load. :type name: str :rtype: bool """ try: loader.get_template(name) return True except TemplateDoesNotExist: return False