예제 #1
0
    def render(self, context):

        kwargs = {key: value.resolve(context) for key, value in self.kwargs.items()}
        try:
            if 'template_name_var' in kwargs:
                extending_template = get_template(kwargs.get('template_name_var'))
            else:
                extending_template = get_template(self.template_name.resolve())
        except:
            return ''

        if self.only:
            flattened_context = kwargs
        else:
            flattened_context = context.flatten()
            flattened_context.update(kwargs)

        nodelist_changed = False
        new_nodelist = self.get_node_list(extending_template.template.nodelist)

        if 'block' in kwargs:
            block_nodes = self.get_all_blocks(new_nodelist)
            new_nodelist = NodeList(
                [node for node in block_nodes if node.name == kwargs.get('block')]
            )
            new_nodelist.contains_nontext = True
            nodelist_changed = True

        if self.multiline:
            blocks = {node.name: node for node in self.nodelist if isinstance(node, BlockNode)}
            self.rearrange_blocks(new_nodelist, blocks)
            nodelist_changed = True

        # This is because there can also be cache loader for template. And if in template loaded with such loader nodes
        # are changed, they are changed in next iteration also.
        # So now I load template from empty string and give him changed nodelist. So I don't change original template
        if nodelist_changed:
            extending_template = template_from_string('', None)
            extending_template.template.nodelist = new_nodelist

        content = extending_template.render(flattened_context)
        content = mark_safe(content)
        return content
예제 #2
0
 def get_node_list(nodelist):
     while len(nodelist) == 1 and isinstance(nodelist[0], ExtendsNode):
         nodelist = nodelist[0].nodelist
     ret = NodeList(nodelist)
     ret.contains_nontext = nodelist.contains_nontext
     return ret