We might wanna create a whole list of questions or checks that an user needs to run by:
In our code however, we shouldn’t be using a separate code for each question, like this:
{% stripnewlines %}
|
| Question
| Extra
{% newline %}
|----5%----
|----45%---
|----------+
{% newline %}
| {% input custom.checklist.check_1 as:boolean %}
| {% t "Has every invoice been delivered by the customer?" %}
| {% input custom.checklist.extra_info_1 as:text size:mini placeholder:"Extra info" %}
{% newline %}
| {% input custom.checklist.check_2 as:boolean %}
| {% t "Are the sales invoices been booked and finalized?" %}
| {% input custom.checklist.extra_info_2 as:text size:mini placeholder:"Extra info" %}
{% endstripnewlines %}
First of all, it’s way too much work writing your code down like this. you can easily make an error where you use the same object for each question, if you’re not carefull.
Also, imagine a whole bunch of questions have to be added; that’s a lot of writing down extra code!
There’s a way to avoid such thing: you can use an “array” and use that array to create one section of code (instead of code for each question), like this:
{% comment %}
use below code to create an array of questions / checks
- the first part is to create the actual questions, separated by a ";"
- the second part is used to loop over each question / check (separated by a ";")
Want to add questions?? => Be sure to add the question in the first part, and add in the second part an unique word (key) for that question.
Respect order in both arrays!!
Split on ";"
{% endcomment %}
{% assign array_checks = "Has every invoice been delivered by the customer?;Are the sales invoices been booked and finalized?;Are the purchase voices been booked and finalized?;Are there any invoices that are related to a previous VAT-period?" | split:";" %}
{% assign array_keys = "delivered;sales_finalized;purchases_finalized;previous_vat" | split:";" %}
<!---------- actual template ---------->
{% stripnewlines %}
|
|
| {% t "VAT-check" %}
| {% t "Extra comments" %}
{% newline %}
|----5%----
|----5%----
|----35%---
|----------+
{% for check in array_keys %}
{% newline %}
| {% input custom.[check].check_mark as:boolean %}
{% comment %}Below will make the templates reconciliation needed{% endcomment %}
{% if custom.[check].check_mark == true %}
{% assign check_ind = 0 %}
{% else %}
{% assign check_ind = 1 %}
{% endif %}
| {% unreconciled check_ind as:indicator %}
| {{ array_checks[forloop.index0] }}
| {% input custom.[check].extra_info as:text size:mini placeholder:"Extra info" %}
{% endfor %}
{% endstripnewlines %}
As you might see, we use only one section of specific code to display our checks and comments:
{% newline %}
| {% input custom.[check].check_mark as:boolean %}
{% comment %}Below will make the templates reconciliation needed{% endcomment %}
{% if custom.[check].check_mark == true %}
{% assign check_ind = 0 %}
{% else %}
{% assign check_ind = 1 %}
{% endif %}
| {% unreconciled check_ind as:indicator %}
| {{ array_checks[forloop.index0] }}
| {% input custom.[check].extra_info as:text size:mini placeholder:"Extra info" %}
Let’s take a deeper look:
We create an array of all our questions we need:
{% assign array_checks = "Has every invoice been delivered by the customer?;Are the sales invoices been booked and finalized?;Are the purchase voices been booked and finalized?;Are there any invoices that are related to a previous VAT-period?" | split:";" %}
Each question is separated by an “;” and we’re going to split on that as well. By splitting it means we’ll be able to loop over that variable (array) and get 4 results (questions).
We create a second array as well, with an unique word or key for each question of the first array:
{% assign array_keys = "delivered;sales_finalized;purchases_finalized;previous_vat" | split:";" %}
Why we do this, becomes clear later on.
We create our header :
{% stripnewlines %}
|
|
| {% t "VAT-check" %}
| {% t "Extra comments" %}
{% newline %}
|----5%----
|----5%----
|----35%---
|----------+
{% endstripnewlines %}
Now, instead of making code for each question / check, we’re going to use one section of specific code which will display all our question out of our array!
If we were be doing this for example:
{% for item in array_keys %}
{{ item }}
{% endfor %}
we’ll get this :
We are going to use a for-loop to get those values from our array array_keys
like this:
{% for check in array_keys %}
{% newline %}
| {% input custom.[check].check_mark as:boolean %}
{% comment %}Below will make the templates reconciliation needed{% endcomment %}
{% if custom.[check].check_mark == true %}
{% assign check_ind = 0 %}
{% else %}
{% assign check_ind = 1 %}
{% endif %}
| {% unreconciled check_ind as:indicator %}
| {{ array_checks[forloop.index0] }}
| {% input custom.[check].extra_info as:text size:mini placeholder:"Extra info" %}
{% endfor %}
This creates a new line for each element of our array, by only using one section of coding!
{% input custom.[check].check_mark as:boolean %}
In each loop, this custom-object is actually custom.delivered.check_mark, custom.finalized.check_mark, … The [check] makes each input custom object in each loop unique.
{{ array_checks[forloop.index0] }}
By using a forloop.index0 on our array array_checks
this wil actually loop through the array along with the original loop.
So the first loop will display “Has every invoice been delivered by the customer?” as object {{ array_checks[forloop.index0] }} ; in the second loop the second question of that array.
The reason we create 2 arrays, is for safety purpose: if we would only use one array and we change the value of an element (because of a typo in a sentence for instance), it’ll cause losing our value we already inputted:
For example: the custom object is called originally custom.Has every invoice been delivered by the cusomer.check_mark
and we input some value in it.
Later on, when I change m array and fix the type from cusomer to customer, my object will be changed to custom.Has every invoice been delivered by the customer.check_mark
but that’s a different object! The value of the other object won’t be displayed again!
Need to create extra questions?
Only change your 2 arrays; for instance let’s add a new question before the last question:
{% assign array_keys = "delivered;sales_finalized;purchases_finalized;NEW_Q;previous_vat" | split:";" %}
We do the same thing for the other array:
{% assign array_checks = "Has every invoice been delivered by the customer?;Are the sales invoices been booked and finalized?;Are the purchase voices been booked and finalized?;THIS IS A NEW QUESTION?;Are there any invoices that are related to a previous VAT-period?" | split:";" %}
which will result in this now:
Want to change a question
Never change the value of the array array_keys when changing questions; only change the elements of the array array_checks !