【问题标题】:How to set up yaml references within a single .yaml file?如何在单个 .yaml 文件中设置 yaml 引用?
【发布时间】:2012-12-05 22:47:32
【问题描述】:

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

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

现在,客户希望能够采用 our_price,添加一些税费,并在 jinja 模板中显示总价。

我想做的是添加一个新属性,所以它看起来像这样:

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

我尝试过使用别名,但似乎它们只能作为节点的整个值。

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

【问题讨论】:

    标签: javascript html yaml jinja2


    【解决方案1】:

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

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

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

    或者,使用别名和锚点,您可以将价格和字符串作为数组带回视图,然后将它们连接成一个字符串:

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

    【讨论】:

      猜你喜欢
      • 2011-01-05
      • 2017-12-21
      • 2018-12-04
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 2010-10-04
      • 2016-02-26
      相关资源
      最近更新 更多