• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
0

Idea: Handling Empty Arrays

Contributor ,
Oct 03, 2015 Oct 03, 2015

Copy link to clipboard

Copied

Is there a way to go back to allowing handling of empty arrays like how JS handles it?

Currently:

{% assign string = "," %}

{% assign stringArr = string | split : "," %}

{% if stringArr[0] %}

   This whole condition doesn't run because stringArr[0] doesn't exist

{% endif %}

Instead:

{% if stringArr[0] %}

     Doesn't run because type if false (ie. undefined);

{% else %}

     Does run because value is undefined.

{% endif %}

So currently in a situation like this:

{%assign  string = ",2,,,5" %}

These are the results.

{{ string[0] }} == '2'

{{ string[1] }} == '5'


Instead of this:


{{ string[0] }} == undefined

{{ string[1] }} == '2'

{{ string[2] }} == undefined

{{ string[3] }} == undefined

{{ string[4] }} == '5'

TOPICS
Developer

Views

867

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 04, 2015 Oct 04, 2015

Copy link to clipboard

Copied

You can not have an array in Liquid Gary that is empty.

In liquid right now values can not be assigned as objects so you split a string or you have collections.

.items in a collection wont exist with no items and you should check the the whole array not to see if it has the first item or not. If you split a string into an array in javascript you will get the basic same output as your describing liquid does, this is not language specific but programming.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 04, 2015 Oct 04, 2015

Copy link to clipboard

Copied

FYI: Just an amendment to one of my lines above as I was referring to splitting a string into an array. Wasn't referring to a collection object.

{%assign  string = ",2,,,5" %}

was suppose to be {%assign  string = ",2,,,5" | split : ',' %}


Liam, if you split an string in JS you get empty values.


So (",2,,,5").split(',') becomes ['','2','','','5']


This is what I'm proposing. The above in liquid returns ['2','5']


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 04, 2015 Oct 04, 2015

Copy link to clipboard

Copied

Not saying this is ideal, but Gary, have you considered a hack such as: (“null,2,null,null,null,5").split(',’) ?

Just throwing that out there

Cheers,

Stephen

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 04, 2015 Oct 04, 2015

Copy link to clipboard

Copied

Yeah .. I have .. the reason why I’m asking if because I’ve had a few situations come of where the empty values are stored and it’s useful to be able to find the position of those value.

For example.

I have a input field where a customer can just duplicate an input filed as many times as they want. This is then store in the one webapp field as a csv. It means I don’t have to create a number of webapp fields to handle multiple data for one particular source. I can iterate through the string.

But when one of the inputs are empty is leaves an empty value in the comma separated string. So it’s useful sometimes to know which position they filled in at.

I’ve got workarounds, but I would love to a more hardcoded approach.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 04, 2015 Oct 04, 2015

Copy link to clipboard

Copied

I'd prefer to keep the current behaviour of split, and have the JS-like behaviour available under a separate filter, such as split_sparse.

                                                                                                                                                                                                                    

sourcesplit: ','split_sparse: ','
''[]['']
','[]['', '']
',,'[]['', '', '']
',,,'[]['', '', '', '']
'a'['a']['a']
'a,'['a']['a', '']
',a'['a']['', 'a']
',,a,,'['a']['', '', 'a', '', '']
',a,,,b,'['a', 'b']['', 'a', '', '', 'b', '']

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 04, 2015 Oct 04, 2015

Copy link to clipboard

Copied

good proposal Rob. I’d be happy with this.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 05, 2015 Oct 05, 2015

Copy link to clipboard

Copied

Valid methods in javascript:

- Grep

- Another array of added values where != undefined or empty (this will work in liquid as a note)

- rejoin and split (will work in liquid) (or split parse as Robert mentioned)

- filter - certain use cases

Language based definitions so your limited. Based on the feedback coming out about Liquid and where it is, I would be surprised if we see any more proper enhancements for a while

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 10, 2016 Mar 10, 2016

Copy link to clipboard

Copied

What do you mean by that " feedback coming out about liquid"? Do you see them losing support for it?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 10, 2016 Mar 10, 2016

Copy link to clipboard

Copied

John, I think Liam was meaning that BC is thinking Liquid's implementation is at a mature level - inferring that perhaps minor updates like these will not happen overnight. Just my take on it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 10, 2016 Mar 10, 2016

Copy link to clipboard

Copied

LATEST

‌Prolly it just made me think of how Parse made all these updates and I guess it didn't go as well as they wanted so Facebook just shut it down.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 09, 2016 Mar 09, 2016

Copy link to clipboard

Copied

Another vote for split_parse!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines