Exploring the additional filters
The additional filters are a set of filters that do not fit under any other filter groups. However, this does not make them any less important. While there are many types of filters that we can name here, we will only mention three of them that are the most essential as we will be using them regularly.
The default filter
As its name suggests, the default filter allows us to set a default value for any variable, whether it is a string, array, or hash type. Note that we can only return the default value if the variable returns nil
, false
, or an empty string. If the variable contains whitespace characters, we will not be able to return the default
value:
Hello {{ customer.name | default: "customer" }}
By introducing the default
value in the previous example, we have ensured that we will not end up with a broken string even if the customer has not provided us with their name. Additionally, we also make our code look a lot cleaner....