Ticket #546: template_loader.2.diff

File template_loader.2.diff, 2.0 KB (added by hugo <gb@…>, 19 years ago)

version of the patch that switches on (list/tuple) or string and uses select_template or get_template

  • template_loader.py

     
    1919    """
    2020    return template.Template(source)
    2121
     22def select_template(template_name_list):
     23    "Given a list of template names, returns the first that can be loaded."
     24    for template_name in template_name_list:
     25        try:
     26            return get_template(template_name)
     27        except template.TemplateDoesNotExist:
     28            continue
     29    # If we get here, none of the templates could be loaded
     30    raise template.TemplateDoesNotExist, ', '.join(template_name_list)
     31
    2232def render_to_string(template_name, dictionary=None, context_instance=None):
    2333    """
    2434    Loads the given template_name and renders it with the given dictionary as
    25     context. Returns a string.
     35    context. Returns a string. If the template_name contains ; it's assumed
     36    that it is a list of different template names and select_template is
     37    used instead of get_template.
    2638    """
    2739    dictionary = dictionary or {}
    28     t = get_template(template_name)
     40    if type(template_name) in (list, tuple):
     41        t = select_template(template_name)
     42    else:
     43        t = get_template(template_name)
    2944    if context_instance:
    3045        context_instance.update(dictionary)
    3146    else:
    3247        context_instance = template.Context(dictionary)
    3348    return t.render(context_instance)
    3449
    35 def select_template(template_name_list):
    36     "Given a list of template names, returns the first that can be loaded."
    37     for template_name in template_name_list:
    38         try:
    39             return get_template(template_name)
    40         except template.TemplateDoesNotExist:
    41             continue
    42     # If we get here, none of the templates could be loaded
    43     raise template.TemplateDoesNotExist, ', '.join(template_name_list)
    44 
    4550class SuperBlock:
    4651    "This implements the ability for {{ block.super }} to render the parent block's contents"
    4752    def __init__(self, context, nodelist):
Back to Top