Skip to content

Add support for is_pattern_library context variable. Fix #156 #167

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions docs/guides/customizing-template-rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ You can for example add a theme wrapper around the components:
<body class="{% block body_class %}{% endblock %}{% if pattern_library_rendered_pattern %} pattern-library-template{% endif %}">
```

## Customizing a single pattern’s rendering
## Customizing a single pattern’s surroundings

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:
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:

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

This is a fair amount of boilerplate, but neatly solves the problem per pattern.

## Customizing a single pattern’s rendering

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.

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:

```django
{% load hub_tags %}

{# Check if this is loading the pattern library or not. #}
{% if not is_pattern_library %}
{% get_hub_menu page as menu %}
{% endif %}

<nav>
<ul>
<li class="hub-menu__list-item">
<a class="hub-menu__link href="{{ menu.parent.url }}">
{{ menu.parent.get_menu_title }}
</a>
</li>
[…]
</ul>
</nav>
```
14 changes: 14 additions & 0 deletions docs/reference/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ tags:
</body>
```

### `is_pattern_library`

`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.

```django
{% if not is_pattern_library %}
{% get_hub_menu page as menu %}
{% endif %}

<a class="hub-menu__link href="{{ menu.parent.url }}">
{{ menu.parent.get_menu_title }}
</a>
```

## Settings

See [Getting started](../getting-started.md) for more guided information.
Expand Down
2 changes: 1 addition & 1 deletion pattern_library/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ def get_sections():


def get_pattern_context_var_name():
return '__pattern_library_view'
return 'is_pattern_library'
13 changes: 13 additions & 0 deletions tests/templates/patterns/atoms/sprites/sprites.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,16 @@
<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"/>
</symbol>
</svg>
{# Show available icons, in pattern library view only #}
{% if is_pattern_library %}
<template>
{% include "patterns/atoms/icons/icon.html" with name="__icon__" %}
</template>
<script>
const template = document.currentScript.previousElementSibling;
const symbols = [...template.previousElementSibling.querySelectorAll('symbol')];
const list = document.createElement('ul');
list.innerHTML = symbols.map(el => `<li>${el.id}&nbsp;${template.innerHTML.replace(/__icon__/g, el.id)}</li>`).join('');
document.body.appendChild(list);
</script>
{% endif %}
2 changes: 1 addition & 1 deletion tests/tests/test_context_modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_applied_by_render_pattern(self, render_to_string):
request=request,
context={
"atom_var": "atom_var value from test_atom.yaml",
"__pattern_library_view": True,
"is_pattern_library": True,
"foo": "bar",
"beep": "boop",
},
Expand Down