-
Notifications
You must be signed in to change notification settings - Fork 334
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
Review and fix Nunjucks HTML indentation #4448
Conversation
📋 StatsFile sizes
Modules
View stats and visualisations on the review app Action run for c6eca9c |
Here's a diff of the HTML changes for all examples (bit big and repetitive) since #4416 |
b14eb6f
to
dc3bd62
Compare
4ea9027
to
c9f891b
Compare
I made a start on review and had began commenting on instances where the indent isn't respected for nunjucks functions like I there something we can do to clean up this extra indentation on render? |
Thanks @owenatgov You mean how I've moved away from indenting Yeah I'd love to have an alternative too, it's definitely a compromise I'll show a few of the options I tried here Indenting both Nunjucks tags and HTMLThis is what we mostly do currently, but indenting each Nunjucks tag level effectively double-indents the HTML Input{% if items | length %}
<ul>
{% for item in items %}
<li>
{% if item.active %}
<a class="active">{{ item.text }}</a>
{% else %}
<a>{{ item.text }}</a>
{% endif %}
</li>
{% endfor %}
</ul>
{% endif %} OutputThe wrapping Plus each Nunjucks tag level adds another 2-space indent <ul>
<li>
<a>One</a>
</li>
<li>
<a class="active">Two</a>
</li>
</ul> Indenting Nunjucks tags onlyThis is what I've tried to do as compromise, i.e. taking Nunjucks tags back an indent level alongside the HTML You'll notice in lots of places though that we nest so deeply it's not possible Input{% if items | length %}
<ul>
{% for item in items %}
<li>
{% if item.active %}
<a class="active">{{ item.text }}</a>
{% else %}
<a>{{ item.text }}</a>
{% endif %}
</li>
{% endfor %}
</ul>
{% endif %} OutputHTML output is correctly 2-space indented <ul>
<li>
<a>One</a>
</li>
<li>
<a class="active">Two</a>
</li>
</ul> Indenting Nunjucks tags with indented macrosAlternatively, using the Summary list approach (with calls to I definitely think this can get too complicated but can be used sparingly When nesting too deeply we'd then need clever combinations of Input{#- Render `<ul>` elements -#}
{%- macro list(items) %}
<ul>
{% for item in items -%}
{{- listItem(item) -}}
{% endfor -%}
</ul>
{% endmacro -%}
{#- Render `<li>` elements -#}
{%- macro listItem(item) %}
<li>
{{ listLink(item) | trim }}
</li>
{% endmacro -%}
{#- Render `<a>` elements -#}
{%- macro listLink(item) -%}
{% if item.active %}
<a class="active">{{ item.text }}</a>
{% else %}
<a>{{ item.text }}</a>
{% endif %}
{%- endmacro -%}
{% if items | length %}
{{- list(items) | trim -}}
{% endif %} OutputHTML output is correctly 2-space indented <ul>
<li>
<a>One</a>
</li>
<li>
<a class="active">Two</a>
</li>
</ul> |
Yeah we currently use But it means we miss HTML formatting issues, because we see the formatted version not the Nunjucks output I've turned the formatter off via c9f891b in this PR (see preview) |
dc3bd62
to
d863a55
Compare
We had a dev-catch up this morning We should still clean up the HTML but restore Nunjucks nesting for readability I'll give this PR an update and move it back into draft for now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this Colin. I've left a bunch of "indent query"'s Where I wanna understand what the impact of retaining the nunjucks indent is to the HTML, if it becomes too wild etc. I've been a bit lazy in how I've identified these lacks of indents because of uncertainties in the problem space itself.
An aside: I'm personally warming to the idea of us authoring our HTML in a more considered way, however I think if our nunjucks indentation is wild enough in a given template that it'll cause problems for our html authoring then we need to:
- find ways to re-structure that given template to reduce the amount of Christmas tree code
- communicate somewhere other than in this PR why we're ignoring nunjucks indentation
Another aside: how difficult would it be do you reckon to write something that automatically re-authored our nunjucks in response to the html output? This is very much spitballing but I wonder if we could set rules for how much html deviates from ideal authorship/indentation and then a script would fix those instances for us. I'd love it if nunjucks automated this at the build phase but it looks like that's just not something nunjucks can do.
packages/govuk-frontend-review/src/common/nunjucks/globals/get-html-code.mjs
Outdated
Show resolved
Hide resolved
<p class="govuk-body">{{ item.content.text }}</p> | ||
{% endif %} | ||
</div> | ||
{% if item %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we indent the nunjucks here or does it make the html too gross?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@owenatgov Instead of indenting more (to add extra spaces to the HTML) how about un-indenting?
Just pushed, here's Accordion:
@@ -9,15 +9,19 @@ | |||
{% set classNames = classNames + " govuk-breadcrumbs--collapse-on-mobile" %} | |||
{% endif -%} | |||
|
|||
<div class="{{classNames}}"{% for attribute, value in params.attributes %} {{attribute}}="{{value}}"{% endfor %}> | |||
<div class="{{ classNames }}" {%- for attribute, value in params.attributes %} {{ attribute }}="{{ value }}"{% endfor %}> | |||
<ol class="govuk-breadcrumbs__list"> | |||
{% for item in params.items %} | |||
{% if item.href %} | |||
<li class="govuk-breadcrumbs__list-item"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indent query here and on line 22
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@owenatgov Same with this one, could we drop the Nunjucks back a level?
Just pushed, here's Breadcrumbs:
</svg> | ||
{% endset %} | ||
{% set classNames = classNames + " govuk-button--start" %} | ||
{% set iconHtml -%} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indent query
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can either have the start button SVG indented too far, or move it into a macro?
Just pushed, here's Button:
</div> | ||
{% endif %} | ||
{% endif %} | ||
{%- for attribute, value in params.attributes %} {{ attribute }}="{{ value }}"{% endfor %} data-module="govuk-checkboxes"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indent query. This one looks like it's going to be tricky so I anticipate being pushed back against.
</h{{ headingLevel }}> | ||
{% endif %} | ||
{% if params.actions.items.length %} | ||
{% if params.actions.items.length == 1 %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ident query. I suspect this one is going to massively indent the summary card markup so I'm open to pushback.
{% endif %} | ||
</div> | ||
{%- set summaryList %} | ||
<dl class="govuk-summary-list {%- if params.classes %} {{ params.classes }}{% endif %}" {%- for attribute, value in params.attributes %} {{ attribute }}="{{ value }}"{% endfor %}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indent query. What's wrong with the minus on the end of the set
statement above?
<dl class="govuk-summary-list {%- if params.classes %} {{ params.classes }}{% endif %}" {%- for attribute, value in params.attributes %} {{ attribute }}="{{ value }}"{% endfor %}> | ||
{% for row in params.rows %} | ||
{% if row %} | ||
<div class="govuk-summary-list__row {%- if anyRowHasActions and not row.actions.items %} govuk-summary-list__row--no-actions{% endif %} {%- if row.classes %} {{ row.classes }}{% endif %}"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indent query
{{ row.value.html | safe | trim | indent(6) if row.value.html else row.value.text }} | ||
</dd> | ||
{% if row.actions.items.length %} | ||
<dd class="govuk-summary-list__actions {%- if row.actions.classes %} {{ row.actions.classes }}{% endif %}"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indent query, same for lines 66 and 68
{% endfor %} | ||
</tr> | ||
{% for row in params.rows %} | ||
{% if row %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indent query
c9f891b
to
b516760
Compare
b516760
to
5ea5cf4
Compare
5ea5cf4
to
7a8beb4
Compare
9ed11f6
to
89599fe
Compare
7a8beb4
to
196ba65
Compare
Other changes to npm packageThe diff could not be posted as a comment. You can download it from the workflow artifacts. Action run for c6eca9c |
35b7a29
to
c6eca9c
Compare
Going to close this one in favour of smaller easier-to-review PRs |
See #4676 (comment) I think app supplied |
This PR updates all Nunjucks templates with correctly indented HTML to close:
indent
in our templates #3211With two caveats:
<textarea>
contentBefore
Checkboxes (without
js-beautify
formatter) from PR #4447https://govuk-frontend-pr-4447.herokuapp.com/components/checkboxes
After
Checkboxes (without
js-beautify
formatter) from this PRhttps://govuk-frontend-pr-4448.herokuapp.com/components/checkboxes