When processing YAML (or JSON), after each render of the template, if there are still variables to replace, add the document to the context and render again, iterating until there are no variables remaining (or some reasonable number of iterations is reached).
Though this is somewhat unnecessary because YAML natively supports references &*, it's a little more straightforward to do it with template replacements and it can support JSON which doesn't provide any way to reference parts of the same document.