Exemplo n.º 1
0
 def test_fragment(self):
     self.assertEqual(
         get_template_ancestors("patterns/atoms/test_atom/test_atom.html"),
         [
             "patterns/atoms/test_atom/test_atom.html",
         ],
     )
Exemplo n.º 2
0
 def test_page(self):
     self.assertEqual(
         get_template_ancestors("patterns/pages/test_page/test_page.html"),
         [
             "patterns/pages/test_page/test_page.html",
             "patterns/base_page.html",
             "patterns/base.html",
         ],
     )
Exemplo n.º 3
0
 def test_page(self):
     self.assertEqual(
         get_template_ancestors('patterns/pages/test_page/test_page.html'),
         [
             'patterns/pages/test_page/test_page.html',
             'patterns/base_page.html',
             'patterns/base.html',
         ],
     )
Exemplo n.º 4
0
 def test_parent_template_from_variable(self):
     self.assertEqual(
         get_template_ancestors(
             "patterns/atoms/test_extends/extended.html",
             context={"parent_template_name": "patterns/base.html"},
         ),
         [
             "patterns/atoms/test_extends/extended.html",
             "patterns/base.html",
         ],
     )
Exemplo n.º 5
0
 def test_parent_template_from_variable(self):
     self.assertEqual(
         get_template_ancestors(
             'patterns/atoms/test_extends/extended.html',
             context={'parent_template_name': 'patterns/base.html'},
         ),
         [
             'patterns/atoms/test_extends/extended.html',
             'patterns/base.html',
         ],
     )
Exemplo n.º 6
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)
    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