Ticket #7815: 7815.tplrf.diff

File 7815.tplrf.diff, 7.0 KB (added by Johannes Dollinger, 16 years ago)

depends on tplrf.2.diff and tplrf-tests.diff from #7806

  • django/template/context.py

    old new  
    99
    1010class Context(object):
    1111    "A stack container for variable context"
    12     def __init__(self, dict_=None, autoescape=True):
     12    def __init__(self, dict_=None, autoescape=True, loader=None):
    1313        dict_ = dict_ or {}
    1414        self.dicts = [dict_]
    1515        self.autoescape = autoescape
     16        self.template_cache = {}
     17        self.loader = loader
    1618
    1719    def __repr__(self):
    1820        return repr(self.dicts)
     
    6567        self.dicts = [other_dict] + self.dicts
    6668        return other_dict
    6769
     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
    6889# This is a function rather than module-level procedural code because we only
    6990# want it to execute if somebody uses RequestContext.
    7091def get_standard_processors():
     
    93114    Additional processors can be specified as a list of callables
    94115    using the "processors" keyword argument.
    95116    """
    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)
    98119        if processors is None:
    99120            processors = ()
    100121        else:
  • django/template/library.py

    old new  
    129129                    dict = func(*args)
    130130
    131131                    if not getattr(self, 'nodelist', False):
    132                         from django.template.loader import get_template, select_template
    133132                        if not isinstance(file_name, basestring) and is_iterable(file_name):
    134                             t = select_template(file_name)
     133                            t = context.select_template(file_name)
    135134                        else:
    136                             t = get_template(file_name)
     135                            t = context.get_template(file_name)
    137136                        self.nodelist = t.nodelist
    138137                    return self.nodelist.render(context_class(dict, autoescape=context.autoescape))
    139138
  • django/template/loader_tags.py

    old new  
    4040class ExtendsNode(Node):
    4141    must_be_first = True
    4242
    43     def __init__(self, nodelist, parent_name, template_dirs=None):
     43    def __init__(self, nodelist, parent_name):
    4444        self.nodelist = nodelist
    4545        self.parent_name = parent_name
    46         self.template_dirs = template_dirs
    4746
    4847    def __repr__(self):
    4948        return '<ExtendsNode: extends "%s">' % self.parent_name
     
    5655        if hasattr(parent, 'render'):
    5756            return parent # parent is a Template object
    5857        try:
    59             source, origin = find_template_source(parent, self.template_dirs)
     58            return context.get_template(parent)
    6059        except TemplateDoesNotExist:
    6160            raise TemplateSyntaxError, "Template %r cannot be extended, because it doesn't exist" % parent
    62         else:
    63             return get_template_from_string(source, origin, parent)
    6461
    6562    def render(self, context):
    6663        compiled_parent = self.get_parent(context)
     
    9188                parent_block.nodelist = block_node.nodelist
    9289        return compiled_parent.render(context)
    9390
    94 class ConstantIncludeNode(Node):
    95     def __init__(self, template_path):
    96         try:
    97             t = get_template(template_path)
    98             self.template = t
    99         except:
    100             if settings.TEMPLATE_DEBUG:
    101                 raise
    102             self.template = None
    103 
    104     def render(self, context):
    105         if self.template:
    106             return self.template.render(context)
    107         else:
    108             return ''
    10991
    11092class IncludeNode(Node):
    11193    def __init__(self, template_name):
     
    11496    def render(self, context):
    11597        try:
    11698            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):
    120101            if settings.TEMPLATE_DEBUG:
    121102                raise
    122103            return ''
     
    178159        {% include "foo/some_include" %}
    179160    """
    180161    template_name = bits.parse_expression(required=True)
    181     if isinstance(template_name, Literal):
    182         # remove ConstantIncludeNode and this hack will be gone
    183         return ConstantIncludeNode(template_name.resolve(None))
    184162    return IncludeNode(template_name)
    185163register.tag('include', token_stream_parsed(do_include))
  • tests/regressiontests/templates/tests.py

    old new  
    634634            'include03': ('{% include template_name %}', {'template_name': 'basic-syntax02', 'headline': 'Included'}, "Included"),
    635635            'include04': ('a{% include "nonexistent" %}b', {}, "ab"),
    636636
     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
    637656            ### NAMED ENDBLOCKS #######################################################
    638657
    639658            # Basic test
Back to Top