I have some variants I’m looping over and then assigning the whole thing to a variable
{% if settings.limit_quantity or settings.display_inventory_left %}
{% assign variantInventory = data-variant-inventory:'[
{% for v in product.variants %}
{
"id":{{ v.id }},
"inventory_quantity":{{v.inventory_quantity}},
"inventory_management":"{{- v.inventory_management -}}",
"inventory_policy":"{{ v.inventory_policy }}"
}
{% if forloop.last == false %},{% endif %}
{% endfor %}
]'%}
{% endif %}
This is then put onto the form as an attribute
{% form 'product', product, id: "product-form-{{ product.id }}", class: "clearfix product_form init {{cssClass}}", variantInventory, data-product-id:"{{ product.id }}"%}
But I’m getting error Liquid syntax error (line 21): ‘endfor’ is not a valid delimiter for if tags. use endif
2
Answers
The problem with your approach is that you are thinking about it like it’s JavaScript.
First things first:
assign myVar = 'hello world' | replace: 'world', ''
But that’s it. You can read more about pipelines here:https://shopify.dev/docs/api/liquid/filters
assign myArray = 'apple,orange,strawberry' | split: ','
. Now you will be able to iterate over your "Array".So to answer your question:
Once you have the variantInventory variable assigned, you can proceed to use it in your form attribute or any other required context.
This should do the trick (not tested):
Then you may use it in your form:
HTH