Add ERB-like trim mode for tags and outputs#214
Add ERB-like trim mode for tags and outputs#214nickpearson wants to merge 2 commits intoShopify:masterfrom
Conversation
|
Hm, that's kind of a big deal, performance-wise :-( |
|
If I remove support for output trimming ( I reran the "before" so the two could be run back-to-back for a good comparison. Before: After: I'll leave it to you on whether this is too big a performance hit for the functionality it provides. If so, maybe there's a way to easily and non-invasively make it opt-in configurable. Or maybe I could move the implementation so that the trimming happens before the parsing, though that seems much more prone to bugs. |
|
I like the feature in principle, but this is definitely too much of an impact and most of Shopify's users don't care about white space in their HTML. Making this opt-in sounds like a good compromise. |
|
I've reimplemented this by handling the trimming before the template is parsed. I'll submit a separate PR momentarily. I'm leaving this open for now in case we decide to come back to it and make it opt-in. |
|
See #215. |
This change adds support for ERB-like trimming to Liquid. Tags and outputs are supported, though they behave differently. In the descriptions below, "newline" means
\nand/or\r(specifically,/\n?\r?/).TAG BEHAVIOR
For any tag starting with
{%-, as with any ERB segment starting with<%-, all tabs and spaces between the beginning of the line and the beginning of the tag will be removed.For any tag ending with
-%}, as with any ERB segment ending with-%>, the newline character immediately following the tag will be removed.For example:
becomes:
Unlike ERB, this implementation for Liquid is more forgiving in that (a) it also removes any tabs and spaces that precede the newline character, and (b) it does not require a newline to be present.
OUTPUT BEHAVIOR
Trim mode for output segments behaves a little differently.
For any output starting with
{{-, all tabs and spaces between the beginning of the line and the beginning of the output and the newline preceding it will be removed. (Tabs and spaces preceding the newline will not be removed.)For any output ending with
-}}, all tabs and spaces and the newline character immediately following the output and all tabs and spaces after the newline will be removed.For example:
becomes:
NOTES
The beginning and ending trim modes can be used independently. For example,
{%- if x %}or{{ x -}}.The reason tabs and spaces preceding the newline in a
{{-trim are not removed is so that whitespace between multiple output segments can be controlled. The reason tabs and spaces following a newline in a-}}trim are removed is so that indentation can be removed. For example, consider the following, where underscores are shown in the place of spaces:becomes:
The space after the comma is kept, but the indentation spaces before 'abc' and 'def' are removed.
BENCHMARKS
Before:
After: