posthtml-expressions
posthtml-expressions copied to clipboard
Is it possible to pass arrays into include locals attribute?
Sorry beginner coder here, this has been driving me nuts though. Is it possible to pass arrays directly in the
You should be able to pass any valid JSON objects, and your JSON object could have an item with an array.
{ "myArray": [1, 2, 3, 4] }
This should works, then you can access it using {{ myArray }} and loop over it using
You should be able to pass any valid JSON objects, and your JSON object could have an item with an array.
{ "myArray": [1, 2, 3, 4] }This should works, then you can access it using {{ myArray }} and loop over it using tag
I guess my question is more how I can I do it in a include? Something like this?
<include src="components/list.html" locals='{list: ["item 1", "item 2"]}'></include> doesn't seem to work?
<include src="components/list.html" locals='{list: ["item 1", "item 2"]}'></include>doesn't seem to work?
It's not working because it's not a valid JSON. JSON mandates that all keys must be strings.
This is valid:
{
"list": [
"item 1",
"item 2"
]
}
So in your case is:
<include src="components/list.html" locals='{ "list": ["item 1", "item 2"] }'></include>
Thanks! That fixed it.