|
| 1 | +# Multiple template variants |
| 2 | + |
| 3 | +See [#87](https://github.com/torchbox/django-pattern-library/issues/87). There is currently no support for trying out a single component with different variations in context or tag overrides, but this can worked around by creating pattern-library-only templates. |
| 4 | + |
| 5 | +For example, for this `call_to_action` template: |
| 6 | + |
| 7 | +```django |
| 8 | +<div class="call-to-action"> |
| 9 | + <img class="call-to-action__image" src="{{ call_to_action.illustration.url }}" alt=""> |
| 10 | + <p class="call-to-action__heading heading heading--three">{{ call_to_action.title }}</p> |
| 11 | + {% include "patterns/atoms/link/link.html" with type="primary" classes="call-to-action__link" href=call_to_action.get_link_url text=call_to_action.get_link_text %} |
| 12 | +</div> |
| 13 | +``` |
| 14 | + |
| 15 | +We can try it out once with the following YAML: |
| 16 | + |
| 17 | +```yaml |
| 18 | +context: |
| 19 | + call_to_action: |
| 20 | + title: Will you help us protect these magnificant creatures in the UK waters? |
| 21 | + illustration: |
| 22 | + url: /static/images/illustrations/sharks.svg |
| 23 | + get_link_text: Sign up for our appeal |
| 24 | + get_link_url: '#' |
| 25 | +``` |
| 26 | +
|
| 27 | +If we want to try multiple variants, simply create a custom template for pattern library usage only, that renders `call_to_action` multiple times: |
| 28 | + |
| 29 | +```django |
| 30 | +<div class="pl-frame pl-frame--white"> |
| 31 | + <h2>Call to action</h2> |
| 32 | + {% for call_to_action in ctas %} |
| 33 | + <div class="pl-row {% if call_to_action.classes %}{{ call_to_action.classes }}{% endif %}"> |
| 34 | + <p>{{ call_to_action.type }}</p> |
| 35 | + {% include "patterns/molecules/cta/call_to_action.html" with call_to_action=call_to_action %} |
| 36 | + </div> |
| 37 | + {% endfor %} |
| 38 | +</div> |
| 39 | +``` |
| 40 | + |
| 41 | +```yaml |
| 42 | +context: |
| 43 | + ctas: |
| 44 | + - type: Call to action |
| 45 | + title: Will you help us protect these magnificant creatures in the UK waters? |
| 46 | + illustration: |
| 47 | + url: /static/images/illustrations/sharks.svg |
| 48 | + get_link_text: Sign up for our appeal |
| 49 | + get_link_url: '#' |
| 50 | + - type: Call to action with short title |
| 51 | + title: Will you help us? |
| 52 | + illustration: |
| 53 | + url: /static/images/illustrations/sharks.svg |
| 54 | + get_link_text: Sign up for our appeal |
| 55 | + get_link_url: '#' |
| 56 | + - type: Call to action with long title |
| 57 | + title: Will you help us protect these magnificant and learn how to make environmentally responsible choices when buying seafood? |
| 58 | + illustration: |
| 59 | + url: /static/images/illustrations/sharks.svg |
| 60 | + get_link_text: Sign up for our appeal |
| 61 | + get_link_url: '#' |
| 62 | +``` |
0 commit comments