CASE: use parts to avoid repetitive coding

Take a look at below table:

It shows a table in which each section is the sum of some ranges. Instead of creating the table in which we assign the same variables over and over again, can we use parts instead to avoid repetitive coding.

An example is given below, in where we need to create the value of current and previous year, while also making sure the section is only printed if there’s a value in current or previous year:

{% comment %}
================
    Pensions
================
{% endcomment %}
{% assign acc_range = "181" %}
{% include "parts/create_values" %}

{% ifi show_values %}
  {% newline %}
  | {% t "provisions_pensions" %}
  | {{ cy_value | currency }}
  {% if prev_y %}
    | {{ py_value | currency }}
  {% endif %}
{% endifi %}

{% comment %}
==============
    Taxes
==============
{% endcomment %}
{% assign acc_range = "182,183" %}
{% include "parts/create_values" %}

{% ifi show_values %}
  {% newline %}
  | {% t "provisions_taxation" %}
  | {{ cy_value | currency }}
  {% if prev_y %}
    | {{ py_value | currency }}
  {% endif %}
{% endifi %} 

As you can see, for each section we refer to a part that has been made especially to create the needed variables:

{% include “parts/create_values” %}

And that part is made like this:

{% comment %}create CY and PY values{% endcomment %}
{% assign cy_value = period.accounts | range:acc_range %}
  {% assign cy_value = -cy_value %}
{% assign py_value = period.minus_1y.accounts | range:acc_range %}  
  {% assign py_value = -py_value %}

{% comment %}check if category needs to be shown or not{% endcomment %}  
{% assign show_values = false %}
{% if cy_value != 0 or py_value != 0 %}
  {% assign show_values = true %}
{% endif %}

Above code gets repeated with every new section (this re-creating needed variables along the way), and the only thing that needs to be updated, is the range of each section (and the title).
Everything else remains the same, while the code still gives a clear view on what is done, and also be easy to maintain in the future :muscle: