Creating a simple loop

Hi,

I’m trying to create a simple loop, where I would like a piece of code to loop X amount of times, based on a number selected in a drop-down field by the user. For example:

{% stripnewlines %}
{% t "Select number of rows:" %}{% input custom.some.thing as:select options:"1|2|3|4|5" %}
{% newline %}

{% input custom.input.row1 placeholder:"Enter text here" %}
{% endstripnewlines %}

If the user selects ‘4’, I would like the custom.input.row1 line to loop four times to create:

  • custom.input.row1
  • custom.input.row2
  • custom.input.row3
  • custom.input.row4

How can I achieve this, and have the key of my drop.name_space.key increment by one as indicated above?

Thanks in advance!
Joe

Hi Joe,

You can try the following:

{% stripnewlines %}
{% t "Select number of rows:" %}{% input custom.some.thing as:select options:"1|2|3|4|5" %}
{% newline %}

{% assign x = custom.some.thing %}

{% for item in (1..x) %}
{% input custom.some.[item] placeholder:"Enter text here" %} 
{% newline %}
{% endfor %}

{% endstripnewlines %}

The key [item] will be 1 on the first iteration of the loop and will keep increasing by one.

Hope that helps,
Borja

Works a charm! Thanks Borja!

Hi @borjaGonzalez,

Just following up on this and was wondering whether you may be able to help with a related issue. I’ve entered my code into a {% for item in (1..x) %} loop, but I seem to have conflicting {% ic %} statements in my code:

{% for number in (1..x) %}
  {% newline %}
  {% ic %}
    <br>
    _{% t "Section" %} {{number}}_
    {% newline %}
    {% input custom.section[number].title_boolean as:boolean %} *{% t "Tick to include title" %}* 
    {% newline %}
  {% endic %}
  
  {% if custom.section[number].title_boolean == true %}
    {::font size="m"}**{% input custom.section[number].title placeholder:"Title" %}**{:/font}
    {% newline %}
  {% endif %}
  
  {% input custom.section[number].text as:text placeholder:"Please input further information"%}
{% endfor %}

I would expect the title and text to appear when printing, as they are outside of {% ic %}, but when print previewing they disappear from my template.

Any ideas?

Thanks!
Joe

Hi Joe,

It is strange as that code should work fine. I have contacted our core developers and they are looking into it but in the meantime a temporary solution that seems to work is adding the following line after {% stripnewlines %}:

&nbsp;

I think the issue might be a conflict between {% stripnewlines %}: and {% ic %} on export.

I will keep you posted.

Best,
Borja

Thanks for getting back to me, @borjaGonzalez. I’ve added &nbsp into my code (directly beneath {% stripnewlines %}), though this just seems to pull through to my template as text and doesn’t resolve the issue. Is there something I am missing?

Cheers
Joe

Sorry Joe, I forgot to add ; at the end. The correct line is:

&nbsp;

Best,
Borja

Also I think another possible issue might be the ‘tabs’ in the text, try to align the code to the left and that might solve the problem too. Something like this:

{% for number in (1..x) %}
{% newline %}
{% ic %}
<br>
_{% t "Section" %} {{number}}_
{% newline %}
{% input custom.section[number].title_boolean as:boolean %} *{% t "Tick to include title" %}* 
{% newline %}
{% endic %}
  
{% if custom.section[number].title_boolean == true %}
{::font size="m"}**{% input custom.section[number].title placeholder:"Title" %}**{:/font}
{% newline %}
{% endif %}
  
{% input custom.section[number].text as:text placeholder:"Please input further information"%}
{% endfor %}

Thanks @borjaGonzalez, though unfortunately neither of these worked :slightly_frowning_face:

Could you please send me the link to your environment?

Thanks,
Borja

Is there a way for me to send this to you directly, or would you like me to post this here?

Thanks
Joe

Hi Joe,

You can just post it here.

Thanks,
Borja

Our environment is https://bishopfleming.getsilverfin.com/. The reconciliation I’ve been referring to can currently be found as a text document titled ‘Executive Summary’ within the client: ‘zTest - Full CoA client’.

Thanks for your help,
Joe

Thank you Joe,

I think I have managed to find a solution. The problem is that you are assigning the variable ‘x’ within {% ic %} tags:

{% ic %}
 {% t "Configure number of sections" %}
 {% input custom.number_of.sections as:select options:"1|2|3|4|5|6|7|8|9|10" default:"1" %}
 {% assign x = custom.number_of.sections %}
{% endic %}

So when you preview the document the program doesn’t read that line. You can just put that line outside the {% ic %} and it should work.

Best,
Borja

Thanks @borjaGonzalez, that has done the trick…!

Although now, when print previewing, I can see both sections that I’ve included; however, upon running the export the first section is lost (the note that says ‘Introduction to your executive summary.’):

Print preview

Exported result

Any ideas why this is being lost in transit?

Thanks for all of your help with this :star2:

Joe

Hi Joe,

Can you check again? I think it’s working now although I am not sure what the problem was.

Best,
Borja

Morning @borjaGonzalez

Yep - all seems to be okay now. Unsure what happened there!

Thanks again :smile:
Joe