Skip to content

Commit b38e834

Browse files
authored
Add support for is_pattern_library context variable. Fix #156 (#167)
1 parent c06f211 commit b38e834

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

docs/guides/customizing-template-rendering.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ You can for example add a theme wrapper around the components:
2222
<body class="{% block body_class %}{% endblock %}{% if pattern_library_rendered_pattern %} pattern-library-template{% endif %}">
2323
```
2424

25-
## Customizing a single pattern’s rendering
25+
## Customizing a single pattern’s surroundings
2626

27-
There is no API to customize a single pattern’s rendering, but it can be done by using pattern-library-only templates. For example, with our `quote_block.html` component:
27+
There is no API to customize a single pattern’s surroundings, but it can be done by using pattern-library-only templates. For example, with our `quote_block.html` component:
2828

2929
```django
3030
<blockquote class="quote-block block--spacing">
@@ -46,3 +46,29 @@ We could create another template next to it called `quote_block_example.html`,
4646
```
4747

4848
This is a fair amount of boilerplate, but neatly solves the problem per pattern.
49+
50+
## Customizing a single pattern’s rendering
51+
52+
Sometimes, it can help for a pattern to work differently in the pattern library. This can be done to make it easier to test, or to avoid rendering parts of a component that have intricate dependencies in the context of the pattern library.
53+
54+
We can do this with the `is_pattern_library` context variable. Here is an example where we bypass loading the real menu data and would instead use the pattern library’s mock context:
55+
56+
```django
57+
{% load hub_tags %}
58+
59+
{# Check if this is loading the pattern library or not. #}
60+
{% if not is_pattern_library %}
61+
{% get_hub_menu page as menu %}
62+
{% endif %}
63+
64+
<nav>
65+
<ul>
66+
<li class="hub-menu__list-item">
67+
<a class="hub-menu__link href="{{ menu.parent.url }}">
68+
{{ menu.parent.get_menu_title }}
69+
</a>
70+
</li>
71+
[…]
72+
</ul>
73+
</nav>
74+
```

docs/reference/api.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ tags:
4242
</body>
4343
```
4444

45+
### `is_pattern_library`
46+
47+
`is_pattern_library` is available in the template context of each pattern, and is `True` if the pattern is being rendered in the pattern library.
48+
49+
```django
50+
{% if not is_pattern_library %}
51+
{% get_hub_menu page as menu %}
52+
{% endif %}
53+
54+
<a class="hub-menu__link href="{{ menu.parent.url }}">
55+
{{ menu.parent.get_menu_title }}
56+
</a>
57+
```
58+
4559
## Settings
4660

4761
See [Getting started](../getting-started.md) for more guided information.

pattern_library/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,4 @@ def get_sections():
6262

6363

6464
def get_pattern_context_var_name():
65-
return '__pattern_library_view'
65+
return 'is_pattern_library'

tests/templates/patterns/atoms/sprites/sprites.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,16 @@
1717
<path d="M1 4.364l3.536 3.535 6.363-6.363" stroke="currentColor" stroke-width="1" fill="none" fill-rule="evenodd" stroke-linecap="round"/>
1818
</symbol>
1919
</svg>
20+
{# Show available icons, in pattern library view only #}
21+
{% if is_pattern_library %}
22+
<template>
23+
{% include "patterns/atoms/icons/icon.html" with name="__icon__" %}
24+
</template>
25+
<script>
26+
const template = document.currentScript.previousElementSibling;
27+
const symbols = [...template.previousElementSibling.querySelectorAll('symbol')];
28+
const list = document.createElement('ul');
29+
list.innerHTML = symbols.map(el => `<li>${el.id}&nbsp;${template.innerHTML.replace(/__icon__/g, el.id)}</li>`).join('');
30+
document.body.appendChild(list);
31+
</script>
32+
{% endif %}

tests/tests/test_context_modifiers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def test_applied_by_render_pattern(self, render_to_string):
124124
request=request,
125125
context={
126126
"atom_var": "atom_var value from test_atom.yaml",
127-
"__pattern_library_view": True,
127+
"is_pattern_library": True,
128128
"foo": "bar",
129129
"beep": "boop",
130130
},

0 commit comments

Comments
 (0)