Ticket #7815: 7815.tplrf.diff
File 7815.tplrf.diff, 7.0 KB (added by , 16 years ago) |
---|
-
django/template/context.py
old new 9 9 10 10 class Context(object): 11 11 "A stack container for variable context" 12 def __init__(self, dict_=None, autoescape=True ):12 def __init__(self, dict_=None, autoescape=True, loader=None): 13 13 dict_ = dict_ or {} 14 14 self.dicts = [dict_] 15 15 self.autoescape = autoescape 16 self.template_cache = {} 17 self.loader = loader 16 18 17 19 def __repr__(self): 18 20 return repr(self.dicts) … … 65 67 self.dicts = [other_dict] + self.dicts 66 68 return other_dict 67 69 70 def get_template(self, template_name): 71 if not template_name in self.template_cache: 72 if self.loader is None: 73 from django.template import loader 74 tpl = loader.get_template(template_name) 75 else: 76 tpl = self.loader.get_template(template_name) 77 self.template_cache[template_name] = tpl 78 return self.template_cache[template_name] 79 80 def select_template(self, template_name_list): 81 from django.template import TemplateDoesNotExist 82 for template_name in template_name_list: 83 try: 84 return self.get_template(template_name) 85 except TemplateDoesNotExist: 86 continue 87 raise TemplateDoesNotExist, ', '.join(template_name_list) 88 68 89 # This is a function rather than module-level procedural code because we only 69 90 # want it to execute if somebody uses RequestContext. 70 91 def get_standard_processors(): … … 93 114 Additional processors can be specified as a list of callables 94 115 using the "processors" keyword argument. 95 116 """ 96 def __init__(self, request, dict=None, processors=None ):97 Context.__init__(self, dict )117 def __init__(self, request, dict=None, processors=None, loader=None): 118 Context.__init__(self, dict, loader=loader) 98 119 if processors is None: 99 120 processors = () 100 121 else: -
django/template/library.py
old new 129 129 dict = func(*args) 130 130 131 131 if not getattr(self, 'nodelist', False): 132 from django.template.loader import get_template, select_template133 132 if not isinstance(file_name, basestring) and is_iterable(file_name): 134 t = select_template(file_name)133 t = context.select_template(file_name) 135 134 else: 136 t = get_template(file_name)135 t = context.get_template(file_name) 137 136 self.nodelist = t.nodelist 138 137 return self.nodelist.render(context_class(dict, autoescape=context.autoescape)) 139 138 -
django/template/loader_tags.py
old new 40 40 class ExtendsNode(Node): 41 41 must_be_first = True 42 42 43 def __init__(self, nodelist, parent_name , template_dirs=None):43 def __init__(self, nodelist, parent_name): 44 44 self.nodelist = nodelist 45 45 self.parent_name = parent_name 46 self.template_dirs = template_dirs47 46 48 47 def __repr__(self): 49 48 return '<ExtendsNode: extends "%s">' % self.parent_name … … 56 55 if hasattr(parent, 'render'): 57 56 return parent # parent is a Template object 58 57 try: 59 source, origin = find_template_source(parent, self.template_dirs)58 return context.get_template(parent) 60 59 except TemplateDoesNotExist: 61 60 raise TemplateSyntaxError, "Template %r cannot be extended, because it doesn't exist" % parent 62 else:63 return get_template_from_string(source, origin, parent)64 61 65 62 def render(self, context): 66 63 compiled_parent = self.get_parent(context) … … 91 88 parent_block.nodelist = block_node.nodelist 92 89 return compiled_parent.render(context) 93 90 94 class ConstantIncludeNode(Node):95 def __init__(self, template_path):96 try:97 t = get_template(template_path)98 self.template = t99 except:100 if settings.TEMPLATE_DEBUG:101 raise102 self.template = None103 104 def render(self, context):105 if self.template:106 return self.template.render(context)107 else:108 return ''109 91 110 92 class IncludeNode(Node): 111 93 def __init__(self, template_name): … … 114 96 def render(self, context): 115 97 try: 116 98 template_name = self.template_name.resolve_safe(context) 117 t = get_template(template_name) 118 return t.render(context) 119 except TemplateSyntaxError, e: 99 return context.get_template(template_name).render(context) 100 except (TemplateDoesNotExist, TemplateSyntaxError): 120 101 if settings.TEMPLATE_DEBUG: 121 102 raise 122 103 return '' … … 178 159 {% include "foo/some_include" %} 179 160 """ 180 161 template_name = bits.parse_expression(required=True) 181 if isinstance(template_name, Literal):182 # remove ConstantIncludeNode and this hack will be gone183 return ConstantIncludeNode(template_name.resolve(None))184 162 return IncludeNode(template_name) 185 163 register.tag('include', token_stream_parsed(do_include)) -
tests/regressiontests/templates/tests.py
old new 634 634 'include03': ('{% include template_name %}', {'template_name': 'basic-syntax02', 'headline': 'Included'}, "Included"), 635 635 'include04': ('a{% include "nonexistent" %}b', {}, "ab"), 636 636 637 'recursive-include': ('{% for item in items %}{{ item.label }}{% if not item.children|length_is:0 %}{% with item.children as items %}({% include "recursive-include" %}){% endwith %}{% endif %}{% endfor %}', { 638 'items': [ 639 {'label': 1, 'children': [ 640 {'label': 2, 'children': [ 641 {'label': 3, 'children': []}, 642 {'label': 4, 'children': []}, 643 ]}, 644 {'label': 5, 'children': [ 645 {'label': 6, 'children': [ 646 {'label': 7, 'children': [ 647 {'label': 8, 'children': []}, 648 ]}, 649 {'label': 9, 'children': []}, 650 ]}, 651 ]}, 652 ]}, 653 ], 654 }, '1(2(34)5(6(7(8)9)))'), 655 637 656 ### NAMED ENDBLOCKS ####################################################### 638 657 639 658 # Basic test