You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Leading whitespace control (e.g. {{- }}, {{%- %}, {#- #}) only strips leading whitespaces until either a non-whitespace character, an Inja construct, or the start of the line, excluding any newline characters.
Trailing whitespace control (e.g. {{ -}}, {{% -%}, {# -#}) strips whitespace until either a non-whitespace character or Inja construct, including any newline characters.
This behavior is inconsistent and a bit surprising.
It would be nice if we had a form of that would only strip until it consumes one newline.
Imagine I have template code like this:
START
{% if exists("children") -%}
{%- for child in children -%}
{{- child }}
{%- endfor -%}
{%- endif %}
END
If children doesn't exist, then I want no additional blank lines emitted - I just want the blank line after START and before END to be emitted. E.g. I want this output if children doesn't exist:
START
END
If I add leading whitespace stripping to the {% if ... -%}, it will not strip the blank line after START:
START
{%- if exists("children") -%}
{%- for child in children -%}
{{- child }}
{%- endfor -%}
{%- endif %}
END
The above code will produce the following output, with three blank lines - the blank line after START (which I want), the blank line before END (which I want) and a blank line from the {%- endif %}.
START
END
But if I add trailing whitespace stripping to the {%- endif %}, it will eat the blank line before END!
START
{%- if exists("children") -%}
{%- for child in children -%}
{{- child }}
{%- endfor -%}
{%- endif -%}
END
Producing this output with only one blank line, the blank line after START:
START
END
In fact, a trailing whitespace control will eat all whitespace until the next non-whitespace character. This is inconsistent with the behavior of a leading whitespace control, which will only eat whitespace until the previous newline. Presumably this is because lines before that have already been output and can't be trimmed.
I can achieve what I want by deleting the newline before END and not using trailing whitespace control on the {%- endif %}, or by adding an empty comment or noop expression on the line before END, but this makes the template code more complicated and harder to read.
It would be better if either:
We fixed trailing whitespace control to consume whitespace characters until the end of the line, including the newline.
We don't change the semantics of the existing trailing whitespace control ({{ -}}, {{% -%}, {# -#}), but we add a new form of trailing whitespace control that only consumes whitespace characters until the end of the line, including the newline.
I think (1) is the cleaner solution, but it'll change existing semantics, and thus might break people's code. This isn't just cosmetic; in some cases, whitespace and blank lines are significant, such as in Markdown.
If we were to go the route of (2), I'd suggest a syntax like /.
Syntax
Semantics
{{- }} {%- %} {#- #}
Strip whitespace characters before the Inja construct until either a non-whitespace character, an Inja construct, or the start of the line, excluding any newline characters. (Status Quo)
{{ -}} {% -%} {# -#}
Strip whitespace characters after the Inja construct until either a non-whitespace character or an Inja construct, including any newline characters. (Status Quo)
{{ /}} {% /%} {# /#}
Strip whitespace characters after the Inja construct until either a non-whitespace character, an Inja construct, or the start of the line, excluding any newline characters. (Proposed)
The text was updated successfully, but these errors were encountered:
Today:
{{- }}
,{{%- %}
,{#- #}
) only strips leading whitespaces until either a non-whitespace character, an Inja construct, or the start of the line, excluding any newline characters.{{ -}}
,{{% -%}
,{# -#}
) strips whitespace until either a non-whitespace character or Inja construct, including any newline characters.This behavior is inconsistent and a bit surprising.
It would be nice if we had a form of that would only strip until it consumes one newline.
Imagine I have template code like this:
If
children
doesn't exist, then I want no additional blank lines emitted - I just want the blank line afterSTART
and beforeEND
to be emitted. E.g. I want this output ifchildren
doesn't exist:If I add leading whitespace stripping to the
{% if ... -%}
, it will not strip the blank line afterSTART
:The above code will produce the following output, with three blank lines - the blank line after
START
(which I want), the blank line beforeEND
(which I want) and a blank line from the{%- endif %}
.But if I add trailing whitespace stripping to the
{%- endif %}
, it will eat the blank line beforeEND
!Producing this output with only one blank line, the blank line after
START
:In fact, a trailing whitespace control will eat all whitespace until the next non-whitespace character. This is inconsistent with the behavior of a leading whitespace control, which will only eat whitespace until the previous newline. Presumably this is because lines before that have already been output and can't be trimmed.
Here's a full Godbolt example demonstrating the problem.
I can achieve what I want by deleting the newline before
END
and not using trailing whitespace control on the{%- endif %}
, or by adding an empty comment ornoop
expression on the line beforeEND
, but this makes the template code more complicated and harder to read.It would be better if either:
{{ -}}
,{{% -%}
,{# -#}
), but we add a new form of trailing whitespace control that only consumes whitespace characters until the end of the line, including the newline.I think (1) is the cleaner solution, but it'll change existing semantics, and thus might break people's code. This isn't just cosmetic; in some cases, whitespace and blank lines are significant, such as in Markdown.
If we were to go the route of (2), I'd suggest a syntax like
/
.{{- }}
{%- %}
{#- #}
{{ -}}
{% -%}
{# -#}
{{ /}}
{% /%}
{# /#}
The text was updated successfully, but these errors were encountered: