Liquid Newline Quirks using include tag
I was creating a CSV export Liquid template for a client. In this template I wanted smart includes to add some dynamic data.
But to my surprise the output of liquid includes added newlines. This is not what you want in a CSV file. Because a newline means the next row in the CSV file.
My code looked like this:
{%- assign complexDataStringsArray = "drilling_info,extra_info,rooting" | split: ',' -%}
{%- for measurement in array -%}
{%- for term in site.csv_content -%}
{%- if complexDataStringsArray contains term -%}
{%- assign path = term | prepend: "includes/csv_export_" -%}
{%- include path -%}
{%- else -%}
{{ measurement[term] }}
{%- endif -%}{%- unless forloop.last -%},{%- endunless -%}
{%- endfor -%}
{%- endfor -%}
See the part {%- include path -%}
? Even when I used the dashed version of the include tag, it still added a newline.
The output of the include part code was always this:
id,drilling_info
,extra_info
,actual text or rooting
So while trying everything like removing al newlines in code and making one big line of code it still added newlines.
Solution
But I did found a solution:
The included file had:
actual text or {{ term }}
And changed this to:
{%- capture rooting -%}
jo mamama
{%- endcapture -%}
{{- rooting -}}
And now the output was:
id,drilling_info,extra_info,jo mamama
Et voila! No newlines anymore. Yeey!
- Next: How to write code documentation in Liquid?
- Previous: Equal Flexbox Columns