CASE: make your code readable with stripnewlines and newline

1. Stripnewlines tag
2. Stripnewlines and tables
3. Newline tag

Making sure your code is well structured is very important in every programming language. It builds logic into your code and you will profit from it in many ways:

  • It will help you to understand your code better
  • You won’t make errors as fast
  • When you do make an error, you’ll find if faster
  • It helps other people to understand your code faster

    1. Stripnewlines tag

Before we go into the newline tag, we’ll have a look at the stripnewlines tag. This tag makes sure that when using indentation, the visualisation of your code is still as you expect. A simple example to show you how this works.

We used following code with and without the stripnewlines tag:

{% stripnewlines %}
{% assign me = true%}
{% if me == true%}
  My name is
{% else %}
  Your name is
{% endif %}

{% endstripnewlines %}

Fig 1.1. Simple code

Fig 1.2 Result without stripnewlines

Fig 1.3 Result with stripnewlines

This is a simple example of how the stripnewlines tag works.

2. Stripnewlines tag and tables

This tag however has one disadvantage. You can’t use this tag on an entire table. Another example will show us why:

Fig 2.1 Simple table code

Fig 2.2 Above code without stripnewlines tag

Fig 2.3 Above code with stripnewlines tag

As you can see, the table is broken by the stripnewlines tag, luckily there’s a solution!

3. Newline tag

Let’s start with another example. Look at the following code:

Fig 3.1 Simple hard to read code

This code shows the price of a product with and without VAT. The tax rate depends on the kind of product. Although the example above is still relatively simple, it’s hard to read. Luckily we can use a combination of the stripnewlines and the newline tag to give structure to our code.

Fig 3.2 Simple easy to read code

The code above is much easier to read. Just use the stripnewlines tag around the entire table and the newline tag everytime you want to start a new row. This will definetly be helpful when facing complex codes.

Fig 3.3 Result

We hope this will help you in the future.
If you have any questions, don’t hesitate to ask them.

Kind regards
The Silverfin Team