How can I check if / else
in yaml file.
like:
if %{attribute}
attributes:
shipping_comment: Shipping comment / Instructions
else
attributes:
shipping_date: Date
In this case, you can embed parameters inside conditions. The script in this YAML file will run because parameters. doThing is true. However, when you pass a parameter to a template, the parameter will not have a value when the condition gets evaluated.
the {{ }} are used to evaluate the expression inside them from the context passed. So {{ '{{' }} evaluates to the string {{
In order to add comments to a YAML file, you simply have to use the # (hashtag symbol) at the start of the line.
YAML is a data serialisation language, so it's not meant to contain if
/else
style executable statements: that's the responsibility of the programming language you're using.
A simple example in Ruby to determine which config string from a YAML file to output could be defining your YAML config file as follows:
data.yml
attributes:
shipping_comment: Shipping comment / Instructions
shipping_date: Date
Then, in your program, read the file in and run the conditional there:
shipping.rb
#!/usr/bin/env ruby
require 'yaml'
config = YAML.load_file('data.yml')
attribute = true # your attribute to check here
if attribute
puts config['attributes']['shipping_comment']
else
puts config['attributes']['shipping_date']
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With