Ticket #546: template_loader.diff
File template_loader.diff, 2.0 KB (added by , 19 years ago) |
---|
-
template_loader.py
19 19 """ 20 20 return template.Template(source) 21 21 22 def 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 22 32 def render_to_string(template_name, dictionary=None, context_instance=None): 23 33 """ 24 34 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. 26 38 """ 27 39 dictionary = dictionary or {} 28 t = get_template(template_name) 40 if template_name.find(';') > 0: 41 t = select_template(template_name.split(';')) 42 else: 43 t = get_template(template_name) 29 44 if context_instance: 30 45 context_instance.update(dictionary) 31 46 else: 32 47 context_instance = template.Context(dictionary) 33 48 return t.render(context_instance) 34 49 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 continue42 # If we get here, none of the templates could be loaded43 raise template.TemplateDoesNotExist, ', '.join(template_name_list)44 45 50 class SuperBlock: 46 51 "This implements the ability for {{ block.super }} to render the parent block's contents" 47 52 def __init__(self, context, nodelist):