While registers are great to store certain numeric values to it, it does have some disadvantages:
- you can not tell by the register which value it contains, which makes it harder to debug eg
- the amount of registers are limited (40)
- if lots of summations are done, it gets complex pretty quickly with all the different registers around
But using registers isn’t something that has to be used, as there is another way that will be more clear to read.
Let’s say we have a table with all values, and at the end we need to show the sum of current and previous year:
{% stripnewlines %}
|----30%----
|----15%----:
|----15%----:
{% comment %}section A{% endcomment %}
{% assign acc_range = "60" %}
{% assign cy_value = period.accounts | range:acc_range %}
{% assign py_value = period.minus_1y.accounts | range:acc_range %}
{% newline %}
| {% t "goods" %}
| {{ cy_value | currency }}
| {{ py_value | currency }}
{% comment %}section A{% endcomment %}
{% assign acc_range = "61" %}
{% assign cy_value = period.accounts | range:acc_range %}
{% assign py_value = period.minus_1y.accounts | range:acc_range %}
{% newline %}
| {% t "costs" %}
| {{ cy_value | currency }}
| {{ py_value | currency }}
{% endstripnewlines %}
output:
now, instead of using registers like $0 and $1, we are going to use plain simple named variables.
Before the table, we’ll do this:
{% comment %}vars to assign totals to{% endcomment %}
{% assign total_cy = 0 %}
{% assign total_py = 0 %}
And then for each section, we will assign the variable to itself and add the part that needs to be added to it:
{% newline %}
| {% t "goods" %}
| {{ cy_value | currency }}
{% assign total_cy = total_cy+cy_value %}
| {{ py_value | currency }}
{% assign total_py = total_py+py_value %}
In fact, this is what a register does too, but the difference here, is that you immediately know which variable holds which value now, which is easier coding and better maintainable
Complete snippet:
{% comment %}vars to assign totals to{% endcomment %}
{% assign total_cy = 0 %}
{% assign total_py = 0 %}
{% stripnewlines %}
|----30%----
|----15%----:
|----15%----:
{% comment %}section A{% endcomment %}
{% assign acc_range = "60" %}
{% assign cy_value = period.accounts | range:acc_range %}
{% assign py_value = period.minus_1y.accounts | range:acc_range %}
{% newline %}
| {% t "goods" %}
| {{ cy_value | currency }}
{% assign total_cy = total_cy+cy_value %}
| {{ py_value | currency }}
{% assign total_py = total_py+py_value %}
{% comment %}section B{% endcomment %}
{% assign acc_range = "61" %}
{% assign cy_value = period.accounts | range:acc_range %}
{% assign py_value = period.minus_1y.accounts | range:acc_range %}
{% newline %}
| {% t "costs" %}
| {{ cy_value | currency }}
{% assign total_cy = total_cy+cy_value %}
| {{ py_value | currency }}
{% assign total_py = total_py+py_value %}
{% comment %}TOTAL{% endcomment %}
{% newline %}
| TOTAL
|^ {{ total_cy | currency }} ^
|^ {{ total_py | currency }} ^
{% endstripnewlines %}