简体   繁体   English

如何在单个.yaml文件中设置yaml引用?

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

I have inherited a large number of .yaml files that contain an attribute like this: 我继承了许多包含如下属性的.yaml文件:

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. 现在,客户希望能够采用our_price,添加一些税费,并在jinja模板中显示总价。

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? 是否可以在YAML中进行设置,或者仅从jinja2的our_price字符串中提取浮点数?

Although I don't know anything about jinja, I think your question is conceptually similar to this StackOverflow question . 尽管我对jinja一无所知,但我认为您的问题在概念上类似于StackOverflow问题

The short answer is, you can't do string interpolation in YAML the way you/your client wants to. 简短的答案是,您不能按照您/您的客户想要的方式在YAML中进行字符串插值。 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: 我相信您将必须从视图文件(或等效的jinja)中将simple_price值传递给our_price值,并将YAML条目更改为以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM