Short code to go from inputlist to outputlist

Hi,

Have a question with regard to this code. It works (on the basis of a couple of questions, you can go from input to output with a minimum of code. Do you see problems with this approach (want to know this before I implement it accross different templates). Thanks!


{% comment %}Geef vragen in (evt dynamisch via result-tag{% endcomment %}

{% stripnewlines %}
{% assign 
  arr_Serie1_StandardTasks_Descriptions 
  = 
"
  Inboeken van de boekhoudkundige documenten.
  |
  Bijstand bij opmaak en indiening van de BTW -aangifte.
  |
  Voorbereiding en indiening van de aangifte bedrijfsvoorheffing.
  |
  Contacten met de bank.
  |
  Betaling van de leveranciersfacturen.
  |
  Betaling van de lonen.
  |
  Opvolging van openstaande klanten.
  |
  Opstellen van een maandelijkse of trimestriële rapportering en bespreking.
  |
  Opstellen van prognoses die maandelijks of trimestrieel worden aangepast."
  | split:"|"
%}
{% endstripnewlines %}

{% comment %}Stel output op{% endcomment %}

{% stripnewlines %}
{% for task in arr_Serie1_StandardTasks_Descriptions %}
  {% ic %}
  {% newline %}
  - {% input custom.arr_Serie1_StandardTasks_Checkbox[forloop.index0] as:boolean default:false %} {{arr_Serie1_StandardTasks_Descriptions[forloop.index0] | strip}}
  {% endic %}
  {% if custom.arr_Serie1_StandardTasks_Checkbox[forloop.index0] == 'true' %}
    {% nic %}
      {% newline %}- {{arr_Serie1_StandardTasks_Descriptions[forloop.index0] | strip}}
    {% endnic %}
  {% endif %}
{% endfor %}
{% endstripnewlines %}

2 Likes

:clap: @Bart_Verhaeghe, really like what you did there! Combine an array with a list of booleans


I would’ve done this though :

{% assign arr_Serie1_StandardTasks_Descriptions = "Inboeken van de boekhoudkundige documenten.|Bijstand bij opmaak en indiening van de BTW -aangifte.|Voorbereiding en indiening van de aangifte bedrijfsvoorheffing.|Contacten met de bank.|Betaling van de leveranciersfacturen.|Betaling van de lonen.|Opvolging van openstaande klanten.|Opstellen van een maandelijkse of trimestriële rapportering en bespreking.|Opstellen van prognoses die maandelijks of trimestrieel worden aangepast." | split:"|" %} 

So you didn’t need to bother to strip the new lines and all, but other than that: :ok_hand:

Also, another thing I learned, is to use a second array to use as an unique key.
Why? You don’t want to find a type in your questions sometime in the future where you want to change that typo. When changing that typo, your “unique” input object will disappear (because the name of that object changes).
See also:

Other than that: great job @Bart_Verhaeghe :muscle:

PS be sure to use tildes instead of quotes to post your code; see also :
https://community.silverfin.com/t/new-in-community-new-to-coding-templates-read-this-first/941/2

:wink:

Hi Sven,

Thanks for your positive comments :-).

Another related question though: I’d like to compose the array with the unique keys on the basis of the original array ‘arr_Serie1_StandardTasks_Descriptions’.

So the array with should be something like ‘StandardTask_Opt0|StandardTask_Opt1|StandardTask_Opt2’ with the number running from 0 to the size of the array ‘arr_Serie1_StandardTasks_Descriptions’. What would be the easiest way to achieve this?

Thanks!

Hmmm, good question @Bart_Verhaeghe

You could create a second array based on the first: if you loop over that first array, and in each loop you create a variable where a fixed name is made, append it with a number and a “;”. That variable can then be used to create the second array.

But, the reason I don’t post an example of this code, is because you can always screw up things if you add a new part into your first array. It’ll affect the second array and a key like “key_5” (where this key was in the 5th loop for instance) can become “key_6” all of a sudden, simply because you added a new part before the 5th part.

That’s why I’d suggest doing it manually with some unique words and/or numbers for each part of your first array. Manually to do, I know, but it can’t be done all automatically. Like your thinking though

Hi @Bart and @sven

The code can be written a little bit shorter.
I’d do it like this:

{% comment %}Geef vragen in (evt dynamisch via result-tag{% endcomment %}

{% stripnewlines %}
{% assign 
  arr_Serie1_StandardTasks_Descriptions 
  = "
  Inboeken van de boekhoudkundige documenten.|
  Bijstand bij opmaak en indiening van de BTW -aangifte.|
  Voorbereiding en indiening van de aangifte bedrijfsvoorheffing.|
  Contacten met de bank.|
  Betaling van de leveranciersfacturen.|
  Betaling van de lonen.|
  Opvolging van openstaande klanten.|
  Opstellen van een maandelijkse of trimestriële rapportering en bespreking.|
  Opstellen van prognoses die maandelijks of trimestrieel worden aangepast." | split:"|"
%}
{% endstripnewlines %}

{% comment %}Stel output op{% endcomment %}

{% stripnewlines %}
{% for task in arr_Serie1_StandardTasks_Descriptions %}
  {% newline %}
  {% ifi custom.arr_Serie1_StandardTasks_Checkbox[forloop.index0] == 'true' %}{% nic %}- {% endnic %}{% input custom.arr_Serie1_StandardTasks_Checkbox[forloop.index0] as:boolean default:false %} {{arr_Serie1_StandardTasks_Descriptions[forloop.index0] | strip}}
  {% endifi %}
{% endfor %}
{% endstripnewlines %}

Escpecially the last part with the ifi is interesting

Kind regards

1 Like

Hi @Sven, Hi @Sam,

Just tested this and the Sven’s remarks are still valid (eg if you mark the first three options to ‘yes’, and then ‘delete’ the second option afterwards, the booleans ‘change’).

So only valid with an ‘unordered list’?

Totally agree that @Sven’s remark is still valid. Just wanted to let you know that you could ifi instead of using ic and nic’s :slight_smile:

OK thanks Sam! Learned something again :slight_smile: