Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Experimenting with a new tag idea #19

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
287 changes: 285 additions & 2 deletions docs/template-tags/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,291 @@

See: [https://design-system.service.gov.uk/components/](https://design-system.service.gov.uk/components/) for more information on the GDS components.

## The gds_component_data tag
The `gds_component_data` templatetag allows you to render any component that has been implemented in the `govuk_frontend_django` package. The only arg taken is the hyphenated name for the component (e.g. "back-link" for the Back Link component). To define the tag data simple add an end tag `endgds_component_data` and the contents of the wrapper should be the Nunjucks dictionary which you can copy directly from the [Components site](https://design-system.service.gov.uk/components/).

Note: variables can still be used, but you need to wrap them in `{{ variable_name }}`.


### Examples:

[https://design-system.service.gov.uk/components/back-link/](https://design-system.service.gov.uk/components/back-link/)
```django
{% gds_component_data "back-link" %}
{
text: "Back",
href: "#"
}
{% endgds_component_data %}
```

[https://design-system.service.gov.uk/components/button/](https://design-system.service.gov.uk/components/button/)
```django
{% gds_component_data "button" %}
{
text: "Save and continue"
}
{% endgds_component_data %}
```

[https://design-system.service.gov.uk/components/tabs/](https://design-system.service.gov.uk/components/tabs/)
```django
{% set pastDayHtml %}{% spaceless %}
<h2 class="govuk-heading-l">Past day</h2>
{% gds_component_data "table" %}
{
head: [
{
text: "Case manager"
},
{
text: "Cases opened"
},
{
text: "Cases closed"
}
],
rows: [
[
{
text: "David Francis"
},
{
text: "3"
},
{
text: "0"
}
],
[
{
text: "Paul Farmer"
},
{
text: "1"
},
{
text: "0"
}
],
[
{
text: "Rita Patel"
},
{
text: "2"
},
{
text: "0"
}
]
]
}
{% endgds_component_data %}
{% endspaceless %}{% endset %}

{% set pastWeekHtml %}{% spaceless %}
<h2 class="govuk-heading-l">Past week</h2>
{% gds_component_data "table" %}
{
head: [
{
text: "Case manager"
},
{
text: "Cases opened"
},
{
text: "Cases closed"
}
],
rows: [
[
{
text: "David Francis"
},
{
text: "24"
},
{
text: "18"
}
],
[
{
text: "Paul Farmer"
},
{
text: "16"
},
{
text: "20"
}
],
[
{
text: "Rita Patel"
},
{
text: "24"
},
{
text: "27"
}
]
]
}
{% endgds_component_data %}
{% endspaceless %}{% endset %}

{% set pastMonthHtml %}{% spaceless %}
<h2 class="govuk-heading-l">Past month</h2>
{% gds_component_data "table" %}
{
head: [
{
text: "Case manager"
},
{
text: "Cases opened"
},
{
text: "Cases closed"
}
],
rows: [
[
{
text: "David Francis"
},
{
text: "98"
},
{
text: "95"
}
],
[
{
text: "Paul Farmer"
},
{
text: "122"
},
{
text: "131"
}
],
[
{
text: "Rita Patel"
},
{
text: "126"
},
{
text: "142"
}
]
]
}
{% endgds_component_data %}
{% endspaceless %}{% endset %}

{% set pastYearHtml %}{% spaceless %}
<h2 class="govuk-heading-l">Past year</h2>
{% gds_component_data "table" %}
{
head: [
{
text: "Case manager"
},
{
text: "Cases opened"
},
{
text: "Cases closed"
}
],
rows: [
[
{
text: "David Francis"
},
{
text: "1380"
},
{
text: "1472"
}
],
[
{
text: "Paul Farmer"
},
{
text: "1129"
},
{
text: "1083"
}
],
[
{
text: "Rita Patel"
},
{
text: "1539"
},
{
text: "1265"
}
]
]
}
{% endgds_component_data %}
{% endspaceless %}{% endset %}

{% gds_component_data "tabs" %}
{
items: [
{
label: "Past day",
id: "past-day",
panel: {
html: '{{ pastDayHtml }}'
}
},
{
label: "Past week",
id: "past-week",
panel: {
html: '{{ pastWeekHtml }}'
}
},
{
label: "Past month",
id: "past-month",
panel: {
html: '{{ pastMonthHtml }}'
}
},
{
label: "Past year",
id: "past-year",
panel: {
html: '{{ pastYearHtml }}'
}
}
]
}
{% endgds_component_data %}
```

## The gds_component tag
The `gds_component` templatetag allows you to render any component that has been implemented in the `govuk_frontend_django` package. The first arg taken is the hyphenated name for the component (e.g. "back-link" for the Back Link component). The remaining args are passed through as keyword arguments to the component.
The `gds_component` templatetag is another step removed from the nunjucks code where you pass the data for the component through as args to the tag.

The tag allows you to render any component that has been implemented in the `govuk_frontend_django` package.The first arg taken is the hyphenated name for the component (e.g. "back-link" for the Back Link component). The remaining args are passed through as keyword arguments to the component.

Examples:

Expand Down Expand Up @@ -45,7 +328,7 @@ Some of the components aren't a simple flat mapping, for these we implement cust
If you use [djLint](https://github.com/Riverside-Healthcare/djLint) to format your templates, you will need to add the following `custom_blocks` to your settings as per the [Custom Blocks documentation](https://www.djlint.com/docs/configuration/#custom-blocks).

```
gds_accordion,gds_accordion_item,gds_breadcrumbs,gds_checkboxes,gds_checkbox_conditional,gds_cookie_banner,gds_cookie_banner_message,gds_error_summary,gds_error_summary_error_list_item,gds_footer,gds_footer_nav,gds_footer_meta,gds_header,gds_header_nav_item,gds_phase_banner,gds_summary_list,gds_summary_list_row,gds_summary_list_row_key,gds_summary_list_row_value,gds_summary_list_row_actions,gds_summary_list_row_actions_item,gds_summary_list_card,gds_summary_list_card_actions,gds_summary_list_card_actions_item,gds_tabs,gds_tabs_tab
gds_component_data,gds_accordion,gds_accordion_item,gds_breadcrumbs,gds_checkboxes,gds_checkbox_conditional,gds_cookie_banner,gds_cookie_banner_message,gds_error_summary,gds_error_summary_error_list_item,gds_footer,gds_footer_nav,gds_footer_meta,gds_header,gds_header_nav_item,gds_phase_banner,gds_summary_list,gds_summary_list_row,gds_summary_list_row_key,gds_summary_list_row_value,gds_summary_list_row_actions,gds_summary_list_row_actions_item,gds_summary_list_card,gds_summary_list_card_actions,gds_summary_list_card_actions_item,gds_tabs,gds_tabs_tab
```

Currently, djLint doesn't support the use of tags acting like either a tag or a block.
Expand Down
2 changes: 1 addition & 1 deletion example_project/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ profile="django"
indent="4"
ignore="H021,H006"
exclude="node_modules,staticfiles,static"
custom_blocks="gds_accordion,gds_accordion_item,gds_breadcrumbs,gds_checkboxes,gds_checkbox_conditional,gds_cookie_banner,gds_cookie_banner_message,gds_error_summary,gds_error_summary_error_list_item,gds_footer,gds_footer_nav,gds_footer_meta,gds_header,gds_header_nav_item,gds_phase_banner,gds_summary_list,gds_summary_list_row,gds_summary_list_row_key,gds_summary_list_row_value,gds_summary_list_row_actions,gds_summary_list_row_actions_item,gds_summary_list_card,gds_summary_list_card_actions,gds_summary_list_card_actions_item,gds_tabs,gds_tabs_tab"
custom_blocks="gds_component_data,gds_accordion,gds_accordion_item,gds_breadcrumbs,gds_checkboxes,gds_checkbox_conditional,gds_cookie_banner,gds_cookie_banner_message,gds_error_summary,gds_error_summary_error_list_item,gds_footer,gds_footer_nav,gds_footer_meta,gds_header,gds_header_nav_item,gds_phase_banner,gds_summary_list,gds_summary_list_row,gds_summary_list_row_key,gds_summary_list_row_value,gds_summary_list_row_actions,gds_summary_list_row_actions_item,gds_summary_list_card,gds_summary_list_card_actions,gds_summary_list_card_actions_item,gds_tabs,gds_tabs_tab"
Loading