posthtml-expressions icon indicating copy to clipboard operation
posthtml-expressions copied to clipboard

Is it possible to pass arrays into include locals attribute?

Open andrew-dotson opened this issue 3 years ago • 1 comments

Sorry beginner coder here, this has been driving me nuts though. Is it possible to pass arrays directly in the tags locals attribute? I don't see any examples on how to do that...and then looping through the values. I know there are other ways to do it but was curious if you could pass it on the include itself.

andrew-dotson avatar May 13 '22 03:05 andrew-dotson

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

thewebartisan7 avatar Sep 23 '22 09:09 thewebartisan7

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?

andrew-dotson avatar Dec 07 '22 22:12 andrew-dotson

<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>

thewebartisan7 avatar Dec 08 '22 01:12 thewebartisan7

Thanks! That fixed it.

andrew-dotson avatar Dec 12 '22 15:12 andrew-dotson