manu
#1
Hi,
I have a data folder setup like:
data
└── marker
├── marker1.toml
├── marker2.toml
├── marker3.toml
And each marker file looks something like:
coordinates = “40, 50”
renderStyle = “pin”
render = false
I’m trying to count the number of markers where render=false.
I tried doing it like this, which gives me the answer I’m looking for:
{{ $counter := int "0" }}
{{ range $.Site.Data.marker }}
{{ if ne .render true }}
{{ $counter = add $counter 1 }}
{{ end }}
{{ end }}
Counter (expected output 2): {{ $counter }} <br/>
I’m having trouble using the “where” keyword to filter through the data.
I tried this, but it gives me 0:
Counter (expected output 2): {{ len (where $.Site.Data.marker ".render" false) }} <br/>
Is it possible to use “where” to get the same result as the first example?
Git repo here:
I’m not in a position to test this myself, but please try render
instead of .render
.
manu
#3
render
gives me the same answer (0) as .render
Here is the output from {{ printf "%#v" $.Site.Data.marker }}
:
map[string]interface {}{
"marker1":map[string]interface {}{"coordinates":"40, 50", "render":false, "renderStyle":"pin"},
"marker2":map[string]interface {}{"coordinates":"50, 60", "render":false, "renderStyle":"pin"},
"marker3":map[string]interface {}{"coordinates":"60, 70", "render":true, "renderStyle":"pin"}
}
From the docs, emphasis mine:
where
filters an array to only the elements containing a matching value for a given field.
1 Like
To follow-up on @pointyfar’s comment, you could use a where clause with this data structure:
[
{
"name": "marker1",
"coordinates": "40, 50",
"render": false,
"renderStyle": "pin"
},
{
"name": "marker2",
"coordinates": "10, 20",
"render": false,
"renderStyle": "pin"
},
{
"name": "marker3",
"coordinates": "30, 40",
"render": true,
"renderStyle": "pin"
}
]