Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split filter doesn't behave as I would expect? #862

Closed
jbsulli opened this issue Feb 13, 2017 · 5 comments
Closed

Split filter doesn't behave as I would expect? #862

jbsulli opened this issue Feb 13, 2017 · 5 comments

Comments

@jbsulli
Copy link

jbsulli commented Feb 13, 2017

I am not sure if this is the right place to report this issue, but Shopify's split filter seems to remove all null strings after the split.

{{ "abcccc" | split:"c" | json}}

This liquid results in ["ab"] and not the ["ab","","","",""] that I would expect.

{{ "abcccc" | split:"c" | size }}

This liquid prints 1. So it's not the json filter.

@adamhollett
Copy link
Contributor

This is expected behaviour, as far as I can tell.

You get the same result with the split method on a string in Ruby.

@jbsulli
Copy link
Author

jbsulli commented Feb 14, 2017

That seems so wrong. Reading about it here, split seems straight up maddening.

For my use case, this behavior will probably be OK. I had to add a separator and additional character to the end to force the correct length and just ignore the last value.

Probably no way of passing a "limit" value to fix this? Looks like passing a negative value for the limit gets me close to what I would expect. Then there's just the null string returning an empty array that I wouldn't expect.

@dylanahsmith
Copy link
Contributor

It just feels wrong for your use case. The behaviour makes more sense for the case where you are splitting on separators between words, where you don't want empty words from multiple separators that are together.

@jbsulli
Copy link
Author

jbsulli commented Feb 14, 2017

Well... to be fair, I am doing something a little crazy. I wrote a snippet that takes html and splits it on tags. For example:

<h1>Hello world!</h1><p>This is a test.</p>

Returns:

["","Hello world!","<p>This is a test.</p>"]

So now I can do something like a page with that html (allowing the content editors to easily modify the html), and then throw it into a popup where the h1 is used as the popup title and the rest of the content is in the body of the popup.

Another (probably better example) is using it for product tabs. So they can use h3 to mark the different tabs. The headline content would go into the tab title and the content following would show when the tab is clicked on.

Gives me a lot of flexibility.

@pushrax
Copy link
Contributor

pushrax commented Jun 3, 2017

I'm not a fan of how Ruby implements split either, but I don't think we will be changing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants