I have a cookiecutter template that I want to render a YAML file based on some options.
As a simple example, suppose a rendered YAML file could look like this:
supported_databases:
- mysql
- postgres
And my cookiecutter JSON looked like this:
{
"mysql": ["yes", "no"],
"postgres": ["yes", "no"]
}
My YAML file is going to be horribly peppered with if
s to support all 4 valid combinations:
{%- if cookiecutter.mysql == 'yes' or cookiecutter.postgres == 'yes' %}
supported_databases:
{%- if cookiecutter.mysql == 'yes' %}
- mysql
{%- endif %}
{%- if cookiecutter.mysql == 'yes' %}
- postgres
{%- endif %}
{%- endif %}
The outer if
is required to prevent invalid YAML being rendered in the case when both options are 'no'.
Is there a neater way to accomplish this?
Thanks for any help.