Skip to content

Commit f76f700

Browse files
committed
Add support for render_patterns wrapping fragments in the base template
1 parent 9969092 commit f76f700

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

pattern_library/management/commands/render_patterns.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from pathlib import Path
22

33
from django.core.management.base import BaseCommand
4+
from django.template.loader import render_to_string
45
from django.test.client import RequestFactory
56

7+
from pattern_library import get_base_template_names, get_pattern_base_template_name
68
from pattern_library.utils import (
7-
get_pattern_templates, render_pattern
9+
get_pattern_templates, get_pattern_context, get_template_ancestors, render_pattern
810
)
911

1012

@@ -27,10 +29,16 @@ def add_arguments(self, parser):
2729
action='store_true',
2830
help="Render the patterns without writing them to disk.",
2931
)
32+
parser.add_argument(
33+
'--wrap-fragments',
34+
action='store_true',
35+
help="Render fragment patterns wrapped in the base template.",
36+
)
3037

3138
def handle(self, **options):
3239
self.verbosity = options['verbosity']
3340
self.dry_run = options['dry_run']
41+
self.wrap_fragments = options['wrap_fragments']
3442
self.output_dir = options['output_dir']
3543

3644
templates = get_pattern_templates()
@@ -44,6 +52,10 @@ def handle(self, **options):
4452
else:
4553
self.stderr.write(f'Target directory: {self.output_dir}')
4654

55+
if self.wrap_fragments:
56+
self.stderr.write('Writing fragment patterns wrapped in base template')
57+
58+
4759
# Resolve the output dir according to the directory the command is run from.
4860
parent_dir = Path.cwd().joinpath(self.output_dir)
4961

@@ -60,7 +72,7 @@ def render_group(self, request, parent_dir: Path, pattern_templates):
6072
self.stderr.write(template.origin.template_name)
6173

6274
render_path = parent_dir.joinpath(template.pattern_name)
63-
rendered_pattern = render_pattern(request, template.origin.template_name)
75+
rendered_pattern = self.render_pattern(request, template.origin.template_name)
6476

6577
if self.dry_run:
6678
if self.verbosity >= 2:
@@ -78,3 +90,24 @@ def render_group(self, request, parent_dir: Path, pattern_templates):
7890
if not self.dry_run:
7991
group_parent.mkdir(exist_ok=True)
8092
self.render_group(request, group_parent, pattern_templates)
93+
94+
def render_pattern(self, request, pattern_template_name):
95+
rendered_pattern = render_pattern(request, pattern_template_name)
96+
97+
# If we don’t wrap fragments in the base template, we can simply render the pattern and return as-is.
98+
if not self.wrap_fragments:
99+
return rendered_pattern
100+
101+
pattern_template_ancestors = get_template_ancestors(
102+
pattern_template_name,
103+
context=get_pattern_context(pattern_template_name),
104+
)
105+
pattern_is_fragment = set(pattern_template_ancestors).isdisjoint(set(get_base_template_names()))
106+
107+
if pattern_is_fragment:
108+
base_template = get_pattern_base_template_name()
109+
context = get_pattern_context(base_template)
110+
context['pattern_library_rendered_pattern'] = rendered_pattern
111+
return render_to_string(base_template, request=request, context=context)
112+
else:
113+
return rendered_pattern

tests/tests/test_commands.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ def test_shows_output_folder(self):
4747
call_command('render_patterns', dry_run=True, stdout=stdout, stderr=stderr, output=temp, verbosity=2)
4848
self.assertIn(temp, stderr.getvalue())
4949

50+
def test_shows_wrap_fragment(self):
51+
stdout = io.StringIO()
52+
stderr = io.StringIO()
53+
call_command('render_patterns', dry_run=True, wrap_fragments=True, stdout=stdout, stderr=stderr, verbosity=2)
54+
self.assertIn('Writing fragment patterns wrapped in base template', stderr.getvalue())
55+
# Only testing a small subset of the output just to show patterns are wrapped.
56+
self.assertIn("""<svg class="icon icon--close" aria-hidden="true" focusable="false">
57+
<use xlink:href="#close"></use>
58+
</svg>
59+
60+
<script src="/static/main.js"></script>
61+
</body>
62+
</html>""", stdout.getvalue())
63+
5064

5165
class RenderPatternsFileSystemTests(SimpleTestCase):
5266
"""Tests of the render_pattern command’s file system changes, based on the test project’s templates"""

0 commit comments

Comments
 (0)