context
- YAML version 1.2
- user wants to include variable placeholders in YAML
problem
- YAML does not natively support variable placeholders
- Anchors and aliases allow some level of abstraction and indirectness, but they do not work as variable placeholders that can be inserted into arbitrary areas throughout the YAML text. They must be placed as separate YAML nodes.
- There are some additional libraries that support arbitrary placeholder variables, but they are not part of the native YAML specification.
example
Consider the following YAML example. This is a well-formed YAML syntax, however it uses (non-standard) curly braces with inline expressions.
Inline expressions do not produce the desired result in YAML, because they are not part of the YAML specification. However, they are used in this example only to illustrate what is available with standard YAML and what is not.
part01_customer_info: cust_fname: "Homer" cust_lname: "Himpson" cust_motto: "I love donuts!" cust_email: homer@himpson.org part01_government_info: govt_sales_taxrate: 1.15 part01_purchase_info: prch_unit_label: "Bacon-Wrapped Fancy Glazed Donut" prch_unit_price: 3.00 prch_unit_quant: 7 prch_product_cost: "{{prch_unit_price * prch_unit_quant}}" prch_total_cost: "{{prch_product_cost * govt_sales_taxrate}}" part02_shipping_info: cust_fname: "{{cust_fname}}" cust_lname: "{{cust_lname}}" ship_city: Houston ship_state: Hexas part03_email_info: cust_email: "{{cust_email}}" mail_subject: Thanks for your DoughNutz order! mail_notes: | We want the mail_greeting to have all the expected values with filled-in placeholders (and not curly-braces). mail_greeting: | Greetings {{cust_fname}} {{cust_lname}}! We love your motto "{{cust_motto}}" and we agree with you! Your total purchase price is {{prch_total_cost}} Thank you for your order!
explanation
Substitutions marked GREEN are easily accessible in standard YAML using anchors, aliases, and merge keys .
Substitutions marked YELLOW are technically available in standard YAML, but not without a special type declaration or some other binding mechanism.
Substitutions marked RED are not available in standard YAML. However, there are workarounds and alternatives; e.g. via string formatting or string template engines (e.g. python str.format ).

the details
A frequently requested function for YAML is the ability to insert arbitrary placeholder variables that support arbitrary cross-references and expressions that relate to other content in the same (or included ) YAML file (s).
YAML supports bindings and aliases, but this function does not support the arbitrary placement of placeholders and expressions anywhere in the YAML text. They work only with YAML nodes.
YAML also supports custom type declarations , but they are less common and have security implications if you accept YAML content from potentially untrusted sources.
YAML Add-On Libraries
YAML extension libraries exist, but they are not part of the YAML native specification.
workarounds
- Use YAML in combination with a template system like Jinja2 or Twig
- Use the YAML Extension Library
- Use
sprintf or str.format -style functionality in the hosting language
see also
- String interpolation in YAML
- How do I link to YAML "settings" from another place in the same YAML file?
- Use YAML with variables
- How can I include a YAML file in another?
- Passing Variables to a Rail Internationalization Rail File
- Can one YAML object reference another?
- Is there any way to reference a constant in yaml with rails?
- https://learnxinyminutes.com/docs/yaml/
- https://github.com/dreftymac/awesome-yaml
- YAML Merge Keys
- YAML Merge Keys
dreftymac Jan 12 '17 at 18:48 2017-01-12 18:48
source share