简体   繁体   中英

Grunt: Access YAML symbols in external file

I am trying to reference values from an external YAML file in my Gruntfile. I am reading the YAML with no problems using:

external_config: grunt.file.readYAML('config.yml')

My config.yml file contains the following:

foo: abc
:bar: def

I can use <% %> templating to get the value for foo in my Gruntfile just fine:

foo: '<%= external_config.foo %>'

But I can't seem to be able to get the value for :bar . I have learned the colon in front of the key denotes a symbol ( Colon in the front: YAML syntax ), but I have no idea how to get to its value from my Gruntfile.

bar: '<%= external_config.bar %>' // doesn't work
bar: '<%= external_config.:bar %>' // throws error "Unexpected token :"
bar: '<%= external_config.\:bar %>' // throws error "Unexpected token :"

I cannot modify the YAML file. Is there a way to do this?

Use external_config[':bar'] within the template.

Explanation: Grunt is just plain JavaScript, so you can access an object's properties by using the bracket notation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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