How to set up yaml references within a single .yaml file?

末鹿安然 提交于 2019-12-13 18:57:30

问题


I have inherited a large number of .yaml files that contain an attribute like this:

our_price: Our price is just <sup>$</sup><span class="amount">99.95</span> this month

Now, the client wants to be able to take our_price, add some taxes and fees, and display a total price in the jinja templates.

What I'd like to do is add a new attribute, so it looks like this:

simple_price: 99.95
our_price: Our price is just <sup>$</sup><span class="amount">simple_price</span> this month

I've tried using aliases, but it seems they only work as the entire value of the node.

Is there a way to set this up in YAML, or a way to pull out just the float from the our_price string in jinja2?


回答1:


Although I don't know anything about jinja, I think your question is conceptually similar to this StackOverflow question.

The short answer is, you can't do string interpolation in YAML the way you/your client wants to. I believe you'll have to pass in the simple_price value to the our_price value from the view file (or jinja equivalent), and change the YAML entry to something like:

simple_price: 99.95
our_price: Our price is just <sup>$</sup><span class="amount">%{simple_price}</span>

Or, using aliases and anchors, you can bring back the price and string as an array to the view, and then join them into a single string there:

simple_price: &simple_price <sup>$</sup><span class="amount">99.95</span>
our_price: 
  - Our price is just
  - *simple_price


来源:https://stackoverflow.com/questions/13937891/how-to-set-up-yaml-references-within-a-single-yaml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!