I’m fairly new in Liquid environment and wonder if you could possibly initiate object variable in Liquid file as similar as JS?
let person = [{
'name' : 'John Doe',
'age' : '20'
}];
I’m aware that liquid is using {% assign %} so is it any working ways of doing
{% assign person = [{
'name' : 'John Doe',
'age' : '20'
}]; %}
This is directly initialise it without pull any data from the metafields.
Thanks in advance!
4
Answers
You cannot create a new custom object in Shopify unless you have saved the object data in a metafield that you have defined and created in the Shopify editor.
The following is a way you can create and assign the object as a string and later on, use javascript to create a javascript object from the string.
You might use sompething like that:
Person is now an array.
or
will output:
name : John Doe
.Then, to go further:
So
or
will output:
John Doe
.etc.
I’m using liquidjs in a non-shopify environment, one solution I came across is using a custom Filter:
idk if you can declare custom Filters in shopify
write your object inside a capture block
then register a new Filter which can create a JS Object from JSON
now you can use your new Filter to work with the returned Object
I actually managed to get a capture (string) into a JS-parsable state.
Later, you can render it in your HTML. Example:
It then can be fetched in JS:
Note that the HTML itself in the page source will be converted, so you will find
"e
tags. They do not prevent you from parsing it and using it in as JavaScript object.I understand that this does not fully answer the question, but as it’s related, I am leaving it here for later readers.