6

How can I set a value in YAML from another key e.g:

example.emails:
     - ben@example.co
     - neb@example.co
     - teb@example.co

swift:
        to_email:   example.emails
Ben_hawk
  • 2,476
  • 7
  • 34
  • 59

2 Answers2

2

Accepted answer is wrong. It might have worked for the author for app specific reason but it is not supported by YAML specs. The correct way to reuse values in yaml is through something called anchors, like this

x1: &my_anchor
  y: 'my val'
x2:
  <<: *my_anchor
  z: 3

In above, we mark the values in x1 using anchor my_anchor. Then the special syntax <<: *my_anchor tells YAML parser to insert children of the node (in this case y) in the same level. So x2 will now have two children: y and z.

Shital Shah
  • 63,284
  • 17
  • 238
  • 185
-1

Ohh found the answer, just could not seem to find good documentation from Google :S

example.emails: 
     - ben@example.co 
     - neb@example.co 
     - teb@example.co 

swift: 
      to_email: "%example.emails%"
Ben_hawk
  • 2,476
  • 7
  • 34
  • 59
  • 1
    I can't tell what this answer actually means. What do the percents (`%`) do? Does this interpolate somehow? A full example would be greatly appreciated. – Benjamin Oakes Apr 30 '14 at 18:59
  • 2
    This is still really unclear to me. An additional example, an demonstration of the result, or a link to the docs where you figured this out would be great. – joe_flash Nov 12 '15 at 17:03
  • 3
    There is nothing in the YAML specification that describes that anything special needs to be done while/after loading this example. If PHP / Synfony does somethings special with `"%example.emails%"` that might be so, but that has nothing to do with the question. – Anthon Nov 02 '18 at 14:26
  • That won't work until `example.emails` is placed as a parameter – Nico Haase Dec 29 '19 at 13:30