Variables linked to each other

Hi there!

I have some variables that need other variables to output the correct data. But I cannot figure this out, because for the first variable, I need something that will be calculated later in my code.

So for example, I have this:

{% assign var1 = (isoc-var3 %}
{% assign var2 = var1/4.21 %}
{% assign var3 = (var1*0.25)-var2 %}

And I cannot change this order, because var3 needs also var2…

In Excel this is working, because there’s no such order to respect.

Any ideas? Is there a way for var1 to read the output of var3 even if it comes later in the code?


hi @Brice

Since you have 3 variables and 3 equations, you could use substitution.
If you substitute the 2nd equation into the 3rd, and the 3rd into the 1st, you could rewrite it like this:

{% assign var1 = (16,84/17,05)*isoc %}

Please let me know if my calculations are correct.

Best regards,

I think I misexplained my situation. Here’s how it looks in Excel:

Capture d’écran 2021-11-26 à 14.38.04

Hi @Brice

Okay, so the same thing, but a little bit more complicated then :sweat_smile:

So this works in Excel because it approximates the values through an iterative calculation which bypasses the circular reference problem from your example.
Unfortunately this is not possible in Liquid and as you’ve noticed, you can’t calculate with variables that are assigned further down in your code.

So I’m afraid the only option you’re left with is basic algebra. Again by substitution you can rewrite your Var3 & Var4 statements so they’re no longer referring to variables further down.
This is what I’ve worked out:

{% assign Var3 = ((b-a+4)*Var1+(2*b-a)*Var2)/(4-b*(a-7)) %}
{% assign Var4 = a*(Var1+2*Var2+Var3)/(8-a) %}

with a = 0,0675 and b = 4,21

Please let me know if this works.


This is working, but I think it will be too complex because the code is not finished yet. For example, var3 is a bit more complex that I wrote here and sometimes in var7 0.25 can be 0.20 and so on… :sweat_smile:

There’s no other alternative to do this?

Hi @Brice

Unfortunately there’s nothing you can do in Liquid to get around the circular references in your variable assignments. The only solution is to rewrite your assignments so there no longer referring to variables that are assigned further down (which themselves refer to variables higher up). Even though the equations are more complex or have more variables, you could still use substitution to resolve this issue.