Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional check in yaml file to show the proper content

Tags:

yaml

How can I check if / else in yaml file.

like:

 if %{attribute}
    attributes:
       shipping_comment: Shipping comment / Instructions
 else
    attributes:
       shipping_date: Date
like image 379
Rajarshi Das Avatar asked May 29 '14 13:05

Rajarshi Das


People also ask

Can we add condition in YAML file?

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.

What does {{ }} mean in YAML?

the {{ }} are used to evaluate the expression inside them from the context passed. So {{ '{{' }} evaluates to the string {{

How do I comment in a YAML file?

In order to add comments to a YAML file, you simply have to use the # (hashtag symbol) at the start of the line.


1 Answers

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
like image 164
Paul Fioravanti Avatar answered Sep 27 '22 20:09

Paul Fioravanti