String interpolation in YAML

I was also looking for this recently for Python and stumbled upon the dynamic-yaml package, through the Awesome YAML repo on Github.

Link Awesome YAML Github Repo: https://github.com/dreftymac/awesome-yaml#parsers Link Dynamic YAML for Python: https://github.com/childsish/dynamic-yaml

Hope this helps!

Edit: Since the comments are correct and links may break, here is a simple example:

If you have a YAML file that looks like this:

house:
    street: cool_street_name
    number: 45
    street_and_number: {house.street}-{house.number}

Install dynamic_yaml with python -m pip install dynamic_yaml to run the following script.

import dynamic_yaml

with open(path_to_yaml_file, 'r') as f:
    data = dynamic_yaml.load(f)

Then you will get:

print(data['house']['street_and_number'])
>> cool_street_name-45

Unfortunately, you're out of luck. To do what you want you'd need to pass in $home from a view file (or wherever) and interpolate it in your yaml entry, which could possibly look something like:

Alice: ! '%{home}/Alice' 

See this StackOverflow Q&A for the detailed answer to pretty much exactly your question.


You should use ERB template.

you can write like following:

Alice: <%=home%>/alice

When use, you need parse home value with ERB before parse as YAML. if home is local variable, you need pass local binding in as #result method's argument. if you not pass this, will use TOP LEVEL binding as default.

Like this:

require 'erb'

home = 'home'
YAML.load(ERB.new(yaml_content).result(binding))