Exemplo n.º 1
0
    def render(self, context):
        if is_pattern_library_context(context):
            template = self.template.resolve(context)
            pattern_context = get_pattern_context(template)
            extra_context = {
                name: var.resolve(context)
                for name, var in self.extra_context.items()
            }

            if self.isolated_context:
                context = context.new()

            merge_pattern_context(context, pattern_context)

            with context.push(pattern_context):
                with context.push(extra_context):
                    original_extra_context = self.extra_context
                    original_isolated_context = self.isolated_context

                    # Force superclass to render with the exact context we've provided.
                    self.extra_context = {}
                    self.isolated_context = False
                    output = super().render(context)

                    # Restore original context variables (nodes are rendered repeatedly inside for loops etc.)
                    self.extra_context = original_extra_context
                    self.isolated_context = original_isolated_context

                    return output

        return super().render(context)
    def render_pattern(self, request, pattern_template_name):
        rendered_pattern = render_pattern(request, pattern_template_name)

        # If we don’t wrap fragments in the base template, we can simply render the pattern and return as-is.
        if not self.wrap_fragments:
            return rendered_pattern

        pattern_template_ancestors = get_template_ancestors(
            pattern_template_name,
            context=get_pattern_context(pattern_template_name),
        )
        pattern_is_fragment = set(pattern_template_ancestors).isdisjoint(
            set(get_base_template_names()))

        if pattern_is_fragment:
            base_template = get_pattern_base_template_name()
            context = get_pattern_context(base_template)
            context['pattern_library_rendered_pattern'] = rendered_pattern
            return render_to_string(base_template,
                                    request=request,
                                    context=context)
        else:
            return rendered_pattern
Exemplo n.º 3
0
    def get(self, request, pattern_template_name=None):
        pattern_template_ancestors = get_template_ancestors(
            pattern_template_name,
            context=get_pattern_context(self.kwargs['pattern_template_name']),
        )
        pattern_is_fragment = set(pattern_template_ancestors).isdisjoint(
            set(get_base_template_names()))

        try:
            rendered_pattern = render_pattern(request, pattern_template_name)
        except TemplateIsNotPattern:
            raise Http404

        if pattern_is_fragment:
            context = self.get_context_data()
            context['pattern_library_rendered_pattern'] = rendered_pattern
            return self.render_to_response(context)

        return HttpResponse(rendered_pattern)
Exemplo n.º 4
0
    def render(self, context):
        if is_pattern_library_context(context):
            parent_context = get_pattern_context(self.parent_name.var)
            if parent_context:
                # We want parent_context to appear later in the lookup process
                # than context of the actual template.
                # So we push parent_context into the beginning of the context stack.
                # See https://docs.djangoproject.com/en/1.11/ref/templates/api/#playing-with-context-objects
                pop_dicts = []
                try:
                    while True:
                        pop_dicts.append(context.pop())
                except ContextPopException:
                    pass

                context.push(parent_context)

                for pop_dict in reversed(pop_dicts):
                    context.push(pop_dict)

        return super().render(context)