Use YAML with variables

前端 未结 5 1017
你的背包
你的背包 2020-11-28 22:47

Are variables within YAML files possible? For example:

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


        
5条回答
  •  再見小時候
    2020-11-28 23:20

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

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

    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):

    Define a value and use an exact copy of it later:

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

    { or }

    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.
    

    For more info, see this section of the wiki page about YAML: http://en.wikipedia.org/wiki/YAML#References

    Define an object and use it with modifications later:

    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
    

    This is taken directly from a great demo here: https://gist.github.com/bowsersenior/979804

提交回复
热议问题