Data Sources
Data sources provide the context for the template engine, i.e. the values to be inserted into the template. To allow a flexible but structured way for values to have defaults, be overridden, or customizable at runtime, contemplate can use multiple data sources at the same time. Each data source can add new values or override previous ones -- the context used by the template engine is the stacked view of all specified data sources. Contemplate makes use of a framework named figment for this purpose.
Command-Line Arguments
Data sources can be specified using command-line arguments, and are evaluated in order, left to right, with later specifications overriding earlier ones.
For example, in the following invocation, values will be taken from the file data.yml
, and environment variables can override these:
The CONTEMPLATE_DATASOURCES
Environment Variable
Data sources can furthermore be specified using the CONTEMPLATE_DATASOURCES
environment variable. This should contain a comma-separated list of data source specifications, each of which is of the form <type>[:<argument>]
:
Data sources specified in the CONTEMPLATE_DATASOURCES
environment variable are evaluated in order left-to-right, just like command-line arguments. However, they can be overridden using command-line arguments.
In the following example, values specified in defaults.yml
will be overridden by values in overrides.yml
:
Data Normalization
Many data sources, e.g. files, support specifying values in a nested-tree format:
Others, such as environment variables, only allow key-value pairs, and are conventionally specified in all-caps:
To bridge this gap, Contemplate will parse the variable names and construct a nested dictionary using the following rules:
- Underscores (
_
) symbolize descent into a nested dictionary - Variable names are converted to lower-case