Skip to content

Commit 6786a1f

Browse files
committed
Add documentation for render_patterns
1 parent e827067 commit 6786a1f

File tree

9 files changed

+164
-46
lines changed

9 files changed

+164
-46
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ The project’s documentation website is built with [MkDocs](https://www.mkdocs.
6464
# One-off build.
6565
poetry run mkdocs build --strict
6666
# Rebuild the docs as you work on them
67-
poetry run mkdocs serve --strict
67+
poetry run mkdocs serve
6868
```
6969

7070
## Running the tests

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Documentation is available at [torchbox.github.io/django-pattern-library](https:
3636
- [Defining template context](https://torchbox.github.io/django-pattern-library/guides/defining-template-context/)
3737
- [Overriding template tags](https://torchbox.github.io/django-pattern-library/guides/overriding-template-tags/)
3838
- [Customizing template rendering](https://torchbox.github.io/django-pattern-library/guides/customizing-template-rendering/)
39-
- [Workflows that work](https://torchbox.github.io/django-pattern-library/guides/workflows-that-work/)
39+
- [Usage tips](https://torchbox.github.io/django-pattern-library/guides/usage-tips/)
4040
- **Reference**
4141
- [API & settings](https://torchbox.github.io/django-pattern-library/reference/api/)
4242
- [Known issues and limitations](https://torchbox.github.io/django-pattern-library/reference/known-issues/)

docs/guides/automated-tests.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Automated tests
2+
3+
Although pattern libraries often start as tools for manual tests during development, they can also be useful for automated UI testing. There are a few benefits to doing UI tests with a pattern library:
4+
5+
- Test the components in isolation. When tests fail, you will know exactly which component has issues, rather than having to inspect whole pages to understand what might have changed.
6+
- Test the components with mock data. One of the issues with UI tests is to have test data for your UIs to render – you can reuse the pattern library data for this purpose (althoug there are [limitations](../guides/multiple-variants.md)).
7+
8+
## Setting up automated UI tests
9+
10+
There are two ways to set up automated tests: by accessing templates as they are rendered by the pattern library directly with Django, or by pre-rendering the templates with [`render_patterns` command](../reference/api.md#render_patterns) and then using this static export to make automated tests.
11+
12+
### Served by Django
13+
14+
Make sure your Django server is up and running, and you can then point your tests directly at the pattern library’s iframe URLs, for example: `http://localhost:8000/pattern-library/render-pattern/patterns/molecules/accordion/accordion.html`.
15+
16+
Note this will always render your templates within the base template ([`PATTERN_BASE_TEMPLATE_NAME`](../reference/api.md#pattern_base_template_name)), which may or may not be appropriate for your tests.
17+
18+
### With `render_patterns`
19+
20+
The [`render_patterns` command](../reference/api.md#render_patterns) command can be used to export all your templates, so you can do bulk checks on them all. For example, testing all templates use valid HTML with the [v.Nu HTML5 validator](https://validator.github.io/validator/):
21+
22+
```sh
23+
./manage.py render_patterns --wrap-fragments
24+
vnu dpl-rendered-patterns/**/*.html
25+
```
26+
27+
One of the advantages of `render_patterns` is the ability for you to test the patterns without wrapping fragments in the base template, should this be more appropriate for your tests.
28+
29+
## Visual regression testing
30+
31+
Pattern libraries are a natural fit for automated visual regression tests. Check out [BackstopJS](https://github.com/garris/BackstopJS) to get started.
32+
33+
## Accessibility testing
34+
35+
Here as well, pattern libraries are a natural fit, due to them providing the test data, and making it possible to test components in isolation. Have a look at [Pa11y](https://pa11y.org/) or [Lighthouse CI](https://github.com/GoogleChrome/lighthouse-ci) to get started.

docs/guides/static-site-export.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Static site export
2+
3+
It can be useful to publish your pattern library as a static site – for example to host it without a runtime dependency on the rest of your code, or to save past versions, or save the output as build artifacts.
4+
5+
## With `render_patterns`
6+
7+
The [`render_patterns` command](../reference/api.md#render_patterns) command can be used to export all your templates, without exporting the pattern library UI.
8+
9+
```sh
10+
# Export all templates, wrapped in the base template like the pattern library UI does.
11+
./manage.py render_patterns --wrap-fragments --output dpl-rendered-patterns
12+
# Or alternatively export all templates without extra wrapping.
13+
./manage.py render_patterns --output dpl-rendered-patterns
14+
```
15+
16+
This command will create a new folder, with a structure matching that of your templates:
17+
18+
```txt
19+
dpl-rendered-patterns
20+
├── atoms
21+
│   ├── icons
22+
│   │   └── icon.html
23+
├── molecules
24+
│   ├── accordion
25+
│   │   └── accordion.html
26+
└── pages
27+
└── people
28+
   └── person_page.html
29+
```
30+
31+
Note this will export all templates but won’t export static files. If you need static files for your export, additionally run [`collectstatic`](https://docs.djangoproject.com/en/3.1/ref/contrib/staticfiles/#collectstatic), and move its output to be where [`STATIC_URL`](https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-STATIC_URL) expects it:
32+
33+
```sh
34+
./manage.py collectstatic
35+
# Moving the collected files to match STATIC_URL
36+
mv static dpl-rendered-patterns/static
37+
```
38+
39+
## With a website scrapper
40+
41+
It’s very straightforward to export the whole pattern library as a static site, including all templates, and the pattern library UI. Here is an example exporting the pattern library with [`wget`](https://en.wikipedia.org/wiki/Wget):
42+
43+
```sh
44+
wget --mirror --page-requisites --no-parent http://localhost:8000/pattern-library
45+
```

docs/guides/usage-tips.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Usage tips
2+
3+
The workflow of developing UI components in a pattern library can be quite different from one-off templates that are only rendered where they are used. Here are tips to make the most of it.
4+
5+
## Keep the pattern library in sync
6+
7+
One of the upsides of having the pattern library built with Django is that the HTML templates can never go out of sync – but the data can! Make sure your template context and tag overrides keep in sync with your actual templates. This can for example be part of a code review checklist.
8+
9+
You may also consider using the [`render_patterns` command](../reference/api.md#render_patterns) to have a baseline check the patterns don’t raise errors while rendering.
10+
11+
## Document your patterns
12+
13+
Patterns support defining a custom `name` in YAML, as well as rendering fully-fledged documentation in Markdown. Create a file next to the template to document it:
14+
15+
```markdown
16+
This template can be used in different places. In streamfield block
17+
or directly in a page template. To use this template pass `call_to_action` into context.
18+
19+
Example:
20+
21+
{% include "patterns/molecules/cta/call_to_action.html" with call_to_action=call_to_action %}
22+
```
23+
24+
## Test faster
25+
26+
One of the main points of maintaining a pattern library is to be able to work on UI components in isolation from where they are used, which is very useful when testing components – access them directly in the pattern library, rather than always having to figure out where they are used, and manually navigating to them.
27+
28+
You can use the pattern library’s iframe view directly to test the component without the pattern library UI, but remember that the pattern will be displayed wrapped in the base template ([`PATTERN_BASE_TEMPLATE_NAME`](../reference/api.md#pattern_base_template_name)), which isn’t always truthful to how components are used in situ.
29+
30+
A pattern library can also help with [automated tests](../guides/automated-tests.md).
31+
32+
## Iterate on components
33+
34+
### Back-end first
35+
36+
Traditionally, Django development starts from models, and everything else is derived from the models’ structure. This is very natural from a back-end perspective – first define your data model, then the view(s) that reuse it, and finally templates.
37+
38+
We generally recommend this approach, but keep in mind that:
39+
40+
- With this workflow it’s natural to write templates that are heavily tied to the database structure, and as such not very reusable, and may be out of touch with visual design.
41+
- There can be work needed later on to reconcile the data structure as defined by the back-end, with what is mandated by the designs.
42+
43+
To mitigate this risk, and overall make templates more reusable, take the time to massage data into basic structures that map better to visual representations.
44+
45+
### Front-end first
46+
47+
Alternatively, the pattern library makes it possible to write templates without models and views. This can be very convenient if your project’s schedule requires this kind of progression.
48+
49+
With this approach, keep in mind that:
50+
51+
- When creating the template from UI principles, there will be assumptions made about the underlying data structures to be provided by Django models. Templates will be heavily tied to their visual design (which generally uses basic data structures like lists and mappings), and may be out of touch with the models once they are created.
52+
- There will be work to do to reconcile the data structure as defined in the UI components, with what is mandated by the models.
53+
54+
Here as well, to mitigate this risk, and overall make templates more reusable, take the time to massage data into basic structures that map better to visual representations.

docs/guides/workflows-that-work.md

Lines changed: 0 additions & 42 deletions
This file was deleted.

docs/reference/api.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,27 @@ PATTERN_LIBRARY = {
116116
"BASE_TEMPLATE_NAMES": ["patterns/base_page.html"],
117117
}
118118
```
119+
120+
## Commands
121+
122+
### `render_patterns`
123+
124+
Renders all django-pattern-library patterns to HTML files, in a directory
125+
structure. This can be useful for [automated tests](../guides/automated-tests.md)
126+
127+
Usage:
128+
129+
```sh
130+
# Render all patterns to the default directory.
131+
./manage.py render_patterns
132+
# Render patterns without outputting files.
133+
./manage.py render_patterns --dry-run
134+
# Render all patterns, with fragment patterns wrapped in the base template.
135+
./manage.py render_patterns --wrap-fragments
136+
# Render patterns to a specific directory.
137+
./manage.py render_patterns --output ./my/path/relative/to/cwd
138+
# Render patterns to stdout.
139+
./manage.py render_patterns --dry-run --verbosity 2
140+
# View all options
141+
./manage.py render_patterns --help
142+
```

mkdocs.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ nav:
3333
# The most important guides are shown in the top-level navigation.
3434
- 'Template context': 'guides/defining-template-context.md'
3535
- 'Overriding tags': 'guides/overriding-template-tags.md'
36-
- 'Workflows that work': 'guides/workflows-that-work.md'
36+
- 'Usage tips': 'guides/usage-tips.md'
3737
- 'Guides':
3838
- 'Customizing rendering': 'guides/customizing-template-rendering.md'
3939
- 'Multiple variants': 'guides/multiple-variants.md'
40+
- 'Automated tests': 'guides/automated-tests.md'
41+
- 'Static site export': 'guides/static-site-export.md'
4042
- 'Reuse across projects': 'guides/reuse-across-projects.md'
4143
- 'Recipes':
4244
- 'Inclusion tags': 'recipes/inclusion-tags.md'

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<svg style="display: none;">
2-
<symbol id="close" width="24" height="24" viewBox="0 0 24 24">
2+
<symbol id="close" viewBox="0 0 24 24">
33
<path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"></path>
44
<path d="M0 0h24v24H0z" fill="none"></path>
55
</symbol>

0 commit comments

Comments
 (0)