mustache / mustache.github.com

The {{official}} website
http://mustache.github.io/
Other
2.29k stars 292 forks source link

use value from list based on another key #124

Closed karlismelderis closed 8 months ago

karlismelderis commented 4 years ago

Hello, Everyone

Sorry if the question is already answered somewhere or it's too trivial. I'm looking for a way to use a value from the list based on another value e.g.

system: {
latest: v2
}

systems: {
v1: {
item: 1
},
v2: {
item: 111
}
}

and in markdown:

{{systems.{{system.latest}}.item}}

:) syntax doesn't work but I hope it will help to show where I'm heading.

jgonggrijp commented 8 months ago

This is not trivial. It is also not a very common use case, so I don't think there are many answers lying around.

I would strongly recommend that you add a latest key to systems, which has a copy/reference to systems[system.lastest], before feeding the data into the template. Preprocessing the data is often the answer to questions like these.

If, for whatever reason, preprocessing the data is really not an option, I don't think there is a standard solution. It seems like a potential use case for dynamic names inside interpolation tags, but this is controversial. You can find previous discussion about this via https://github.com/mustache/spec/pull/134.

Closing this, as this repository is only about the Mustache website, not about the template language.