Is there a way to design a form template that is used by content pages with various parameters? Something like this:
_layouts/form.html
---
layout: default
---
<form method="POST" action="/info/{{ page.action }}">
{% for question in page.questions %}
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="question{{ question.id }}">
<label class="form-check-label" for="question{{ question.id }}">{{ question.text }}</label>
</div>
{% endfor %}
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<script>
$("form").submit(function(event) {
var inputElems = $("input"),
count = 0;
for (var i=0; i<inputElems.length; i++) {
if (inputElems[i].type === "checkbox" && inputElems[i].checked === true){
count++;
}
}
if (count > {{ page.count }}) {
return;
} else {
event.preventDefault();
alert("Alert");
}
});
</script>
And a content page could consist of only a front matter that defines variables used by the template:
---
layout: form
title: Title
action: /results
questions: site.data.questions1
count: 10
---
The nicer solution would be to create some YAML file with variables definitions that are injected into the template accounting for the permalink of the form page. This file would be used to generate the end form pages.