Assigning value within ic-statement to variable outside ic-statement

Hi,

I’m having an issue with assigning a value in an ic-statement to a variable that has been created outside that variable.

Here an example of what I am trying to do:

{% assign testVar = false %}

{{ testVar }}

{% ic %}{% input period.custom.doc.received as:boolean default:testVar assign:testVar %}{% endic %}

{{ testVar }}

In edit mode I see “true” for the 2nd variable but in preview I see 2x false. Isn’t it supposed to show ‘true’ the 2nd time since the variable has been created outside the ic-statement? It is like the value is not being assigned.

Screenshot 2021-07-15 at 16.46.40
Screenshot 2021-07-15 at 16.47.11

Thanks!

Best regards,
Gianluca

Hi @Gianluca,

I can simulate your issue, as if I reproduce your code, your boolean value will have the value false (as your first var is created no matter what).

Your second var testVar however, will only be created in input-mode however, which is the issue you’re facing :point_up:

but you have to watch out with coding like this. I’d advise you to always create vars outside IC-tags. Yes, that means an extra line of coding in this case, as you cannot use the assign attribute as that would mean the creation of the variable (that has to work, no matter IC or NIC) is only done in input-mode:

{% comment %}create needed vars{% endcomment %}
{% assign doc_received = period.custom.doc.received | default:false %}

{% ic %}{% input period.custom.doc.received as:boolean default:false %}{% endic %}

{{ doc_received }} 

Also, keep the naming of vars limited to just small letters (see here our guidelines about that); just a tip though :wink:

1 Like

Hi @sven

Thanks for your quick reply! Your code is working but I’m not sure what the difference is with mine?
I see you are using the value of the global variable, in the local variable instead of “assign” but the outcome is the same, no? Isn’t it supposed to work how I did it?

Thanks!

No, because your assign statement is done within IC-tags, meaning the var testVar will be created only in input-mode.
If you then export the template (export mode), that variable isn’t created anymore.

This would be fine:

{% input period.custom.doc.received as:boolean default:false assign:doc_received %}

as it gets created always (so not just in IC mode).

This however:

{% ic %}{% input period.custom.doc.received as:boolean default:false assign:doc_received %}{% endic %}

will only work in IC-mode as your assign-statement is done in IC-mode. Hence, you cannot really use that assign within the input-statement IF that input-tag is also between IC-tags.

Hope that clarifies it; if not, don’t shy away to ask :slightly_smiling_face:

Alright thanks for the explanation! :slightly_smiling_face:

1 Like