Shopify / liquid

Liquid markup language. Safe, customer facing template language for flexible web apps.
https://shopify.github.io/liquid/
MIT License
11.03k stars 1.38k forks source link

Custom Liquid tags often add a trailing space to their arguments #968

Open adamhollett opened 6 years ago

adamhollett commented 6 years ago

@jonniesweb and I are working on some custom Liquid tags for a site that can divide a section of content into small chunks, each of which has a button you can click to view that section of content. We use it to separate different sets of instructions by device (so we don't have to show instructions for desktop, iOS, and Android at the same time). It's kind of like a tabbed carousel.

Our tag looks like this:

{% sections Desktop, iOS, Android %}

Content for desktop only

----

Content for iOS only

----

Content for Android only

{% endsections %}

We pass a list of the device types as an argument and split it by commas.

The Liquid for Programmers wiki article says that arguments are passed to the tag "as a single string" which is true and works fine but this string includes the space before the tag termination pattern %}.

So when my tag is written {% sections Desktop, iOS, Android %}, the argument that gets passed is 'Desktop, iOS, Android ', with a trailing space. Liquid is interpreting the single space following "Android" as part of the argument, and as a result I need to #strip my argument string before I do anything with it. This seems like it shouldn't be happening.

pushrax commented 6 years ago

Ideally, tags would have a defined syntax grounded in some sort of consistent expression system. https://github.com/Shopify/liquid/issues/560 was an attempt at moving in that direction, but we never acted on it.

Without quote characters it's simply ambiguous; it would feel like an arbitrary choice to automatically #strip the argument string.

adamhollett commented 6 years ago

OK, this is going to really show how junior I am, but what would happen if we just added a single optional space to the TagEnd regex?

I agree that #striping the argument is probably a bad thing to default to, but I think it can be safely said that almost everyone who writes Liquid pads their variables and tags with spaces, so this extra space thing affects almost all custom tags.