Code syntax

In this sections you will learn some punctual tips regarding how to style your code to improve it's readability.

Indentation

Use indent to make it clear where e.g.. an if statement starts and ends. Indenting like the following example:

{% assign show_message = "indenting is great" %}
{% for item in collection %}
    {% if item == true %}
        {% ic %}
            {{ show_message }}
        {% endic %}
    {% endif %}
{% endfor %}

Spacing

To get a more clear and clean view of your code, use spaces between your code tags and it's content. For example:

Do: {% assign test = "content" %} 
 don´t do: {%assign test="content"%}

Booleans

When using true/false never make it a string (e.g. when using an if-statement)

Don´t do:

{% assign Myvar = "true" %}

Do: 

{% assign Myvar = true %}

Assigning variables with a default

When assigning a custom or local variable the logical output may seem to be the following;

{% assign lux_aa_settings = period.reconciliations.lux_aa_settings %}

{% assign chosen_comp_form_aa_settings = lux_aa_settings.custom.aa_settings.comp_form | default:comp_form_value %}

The main assumption would be that if the template lux_aa_settings does not exist that the default variable comp_form_value would be assumed but this is not the case in liquid. But the truth is that, if the template doesn’t exist, we would get a liquid error while we do this assignment. To avoid a possible liquid error in this case the correct syntax would be as follows.

{% if lux_aa_settings.exists? %}
  {% assign chosen_comp_form_aa_settings = lux_aa_settings.custom.aa_settings.comp_form | default:comp_form_value %}
{% else %}
  {% assign chosen_comp_form_aa_settings = comp_form_value %}
{% endif %}

Here we check if the template does or does not exist. In the case the template does not exist we can assign comp_form_value to chosen_comp_form_aa_settings

Boolean with variable assigned to false as default

In Silverfin insights there is the possibility to have a segment which is based upon the whether a checkbox has been ticked off or not.

In order to have this segment you need to have a result in the related reconciliation that has a value true or false. By default a boolean is blank. To avoid a blank result the following logic ought to be followed.

{% input custom.reviewed.check as:boolean %} {% t "Has this been reviewed?" %}

{% comment %}
create result for SF Insights
{% endcomment %}
{% assign reviewed = custom.reviewed.check | default:false %}
{% result 'reviewed' reviewed %}

The variable is assigned to the real value custom.reviewed.check or is assigned too false in the case the database variable is blank. The boolean can have three possible values in the database true, false or blank. Though it is important to remember from the user perspective there are only two options and thus is is important to assign it too false when the outcome it blank so that Silverfin insights can pick up this value as is searches for a yes or no value.