本文介绍了如何在单个.yaml文件中设置yaml引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了大量包含如下属性的.yaml文件:

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

现在,客户希望能够获取my_price,增加一些税费,以及在jinja模板中显示总价格。

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.

有没有办法在YAML中设置它,或者从jinja2中的our_price字符串中提取浮点数?

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?

推荐答案

虽然我对jinja一无所知,但我认为你的问题在概念上类似于。

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

简短的回答是,你不能像你/你的客户那样在YAML中进行字符串插值想要。我相信你必须将 simple_price 值传递给视图文件中的 our_price 值(或等效的jinja) ),并将YAML条目更改为:

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

这篇关于如何在单个.yaml文件中设置yaml引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 11:27