将 YAML 与变量一起使用 [英] Use YAML with variables

查看:75
本文介绍了将 YAML 与变量一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

YAML 文件中是否可以使用变量?例如:

Are variables within YAML files possible? For example:

theme:
  name: default
  css_path: compiled/themes/$theme.name
  layout_path: themes/$theme.name

在这个例子中,如何在其他设置中使用theme: name: default?什么是语法?

In this example, how can theme: name: default be used in other settings? What is the syntax?

推荐答案

我也有同样的问题,经过大量研究,看起来不可能.

I had this same question, and after a lot of research, it looks like it's not possible.

来自 cgat 的答案是正确的,但您实际上无法连接这样的引用.

The answer from cgat is on the right track, but you can't actually concatenate references like that.

您可以使用 YAML 中的变量"执行以下操作(设置它们时正式称为节点锚点",稍后使用它们时称为引用"):

Here are things you can do with "variables" in YAML (which are officially called "node anchors" when you set them and "references" when you use them later):

default: &default_title This Post Has No Title
title: *default_title

{ 或 }

example_post: &example
  title: My mom likes roosters
  body: Seriously, she does. And I don't know when it started.
  date: 8/18/2012
first_post: *example
second_post:
  title: whatever, etc.

有关更多信息,请参阅维基页面关于 YAML 的这一部分: http://en.wikipedia.org/wiki/YAML#References

default: &DEFAULT
  URL:          stooges.com
  throw_pies?:  true  
  stooges:  &stooge_list
    larry:  first_stooge
    moe:    second_stooge
    curly:  third_stooge

development:
  <<: *DEFAULT
  URL:      stooges.local
  stooges: 
    shemp: fourth_stooge

test:
  <<: *DEFAULT
  URL:    test.stooges.qa
  stooges: 
    <<: *stooge_list
    shemp: fourth_stooge

这直接取自此处的精彩演示: https://gist.github.com/bowsersenior/979804

这篇关于将 YAML 与变量一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆