Skip to content

Commit

Permalink
Add ids to add another and check your answers
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldfallen committed Feb 1, 2018
1 parent 3248b8e commit 45cb2ad
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/nunjucks.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ const nunjucksDefaults = {
},
isBoolean(value) {
return typeof value === 'boolean';
},
safeId(id) {
return id.toString()
.toLowerCase()
// replace foo[1] to foo-1
.replace(/\[(\d{1,})\]/, '-$1')
.replace(/[^A-Za-z0-9\s_-]/g, '')
// replace 'foo bar' to 'foo-bar'
.replace(/\s/g, '-');
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion templates/look-and-feel/components/check-your-answers.njk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% macro answer(question, answer, url) %}
<div>
<div id="cya-{{ safeId(question) }}">
<dt class="cya-question">
{{ question }}
</dt>
Expand Down
2 changes: 1 addition & 1 deletion templates/look-and-feel/layouts/add_another.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

{# Macro specific to this layout #}
{% macro addAnotherItem(field, deleteUrl, editUrl) %}
<div>
<div {% if field %} id="add-another-list-{{ safeId(field.id) }}" {% endif %}>
<dd class="add-another-list-item {{ errorClass(field) }}">
{{ errorsFor(field) }}
{{ caller() }}
Expand Down

0 comments on commit 45cb2ad

Please sign in to comment.