con2 / emrichen

A Template engine for YAML & JSON
MIT License
107 stars 11 forks source link

Dynamically generated dict keys #29

Closed saladpanda closed 5 years ago

saladpanda commented 5 years ago

I'm trying to use emrichen as a preprocessor for HomeAssistant configuration files. I need to be able to generate YAML-code like this:

homeassistant:
  customize:
    light.asdf:
      hidden: true
    light.foobar:
      hidden: true

My current emrichen template looks like this:

!Defaults
lamps:
  - {name: asdf, id: 123}
  - {name: foobar, id: 456}
---
homeassistant:
  customize:
    !Loop2
      over: !Var lamps
      as: lamp
      template:
        !Format "light.{lamp.name}":
          hidden: true

Is there a way to achieve this?

japsu commented 5 years ago

The tag you are looking for is !Index.

!Defaults
lamps:
  - {name: asdf, id: 123}
  - {name: foobar, id: 456}
---
homeassistant:
  customize:
    !Index
      over: !Var lamps
      as: lamp
      by: !Format light.{lamp.name}
      template:
        hidden: true