Skip to content

for tag does not support valueOf recognition in Drop #515

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

Closed
maiyuanrdt opened this issue Jul 5, 2022 · 4 comments · May be fixed by ASISBusiness/docs#17 or ReneMaetzschker/docs#2
Closed

for tag does not support valueOf recognition in Drop #515

maiyuanrdt opened this issue Jul 5, 2022 · 4 comments · May be fixed by ASISBusiness/docs#17 or ReneMaetzschker/docs#2

Comments

@maiyuanrdt
Copy link

Shopify Code example:

<ul> 
  {%- for field in form.errors -%}
    <li>
      {%- if field == 'form' -%}
        {{ form.errors.messages[field] }}
      {%- else -%}
        <a href="#RegisterForm-{{ field }}">
          {{ form.errors.translated_fields[field] | capitalize }}
          {{ form.errors.messages[field] }}
        </a>
      {%- endif -%}
    </li>
  {%- endfor -%}
</ul>

The above is the liquid code structure that outputs the error information in the form form in Shopify, in which for is used to traverse form.errors and use it as an index key.
The result of execution in Shopify is:
origin_img_v2_ac6c3940-e11b-42e2-b06f-03de03104deg
and
origin_img_v2_a4e5db1d-0d8a-43cb-b204-320769d92d8g

-------------------------------------------------------------problem lies in------------------------------------------------------------
Using liquidjs for simulation adaptation, it was found that the valueOf in Drop was not responded to.

class ErrorsDrop extends Drop {
    constructor() {
        super()
        this.password = 'pppp'
        this.email = 'eeee'
    }

    valueOf() {
        return ['xxx', 'ccc']
    }
}
const errors = new ErrorsDrop()
liquid.parseAndRender('-- This {{ name }}: {{errors}} -- {% for field in errors %} ={{ field }}= {% endfor %}', {
    name: 'valueOf',
    errors
})

I defined an object named ErrorsDrop, assigned it to the errors variable, and traversed the errors through the for tag, and got the following results:

-- This valueOf: xxxccc --  =passwordpppp=  =emaileeee= 

Personally think that when for in is followed by a Drop object, it should respond to the result after its valueOf(). Otherwise, writing similar to Shopify will not take effect

@maiyuanrdt
Copy link
Author

@harttle Hi~

@harttle
Copy link
Owner

harttle commented Jul 7, 2022

Try adding json filter as you’re in Shopify template

This {{ name }}: {{errors|json}} -- {% for field in errors %} ={{ field|json }}= {% endfor %}', {
    name: 'valueOf',

The way LiquidJS iterate objects can be different. I can try fix it as a bug.

@harttle harttle added the bug label Jul 7, 2022
@harttle harttle closed this as completed in c3e51ca Jul 9, 2022
github-actions bot pushed a commit that referenced this issue Jul 9, 2022
# [9.39.0](v9.38.0...v9.39.0) (2022-07-09)

### Bug Fixes

* for tag not respecting Drop#valueOf(), fixes [#515](#515) ([c3e51ca](c3e51ca))

### Features

* iteration protocols ([a19feea](a19feea))
@github-actions
Copy link

github-actions bot commented Jul 9, 2022

🎉 This issue has been resolved in version 9.39.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@harttle
Copy link
Owner

harttle commented Jul 9, 2022

Try v9.39.0, now LiquidJS aligns with shopify in treating valueOf (to_liquid_value in Ruby):

c3e51ca#diff-a78edf8898b08c521478be171016452cfe322e302f08d167e0f931376b2e7d68R64-R74

For Drop iteration, maybe you're interested in this:

class MockIterableDrop extends Drop {
* [Symbol.iterator] () {
yield 'a'
yield 'b'
yield 'c'
}
toString () {
return 'MockIterableDrop'
}
}

Note that if you decide to use valueOf, you'll have to implement the Symbole.iterator on the return value of valueOf.

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