Page tree
Skip to end of metadata
Go to start of metadata

This page is about using YAML in Magnolia. YAML is a data serialization format designed for human readability and interaction with scripting languages. Use YAML to configure and define apps, dialogs, field types, message views and templates, media editor and renderers. You can define these same items using JCR nodes via Configuration app; YAML is just an additional possibility.

Syntax

  • Whitespace indentation is used to denote structure; however tab characters are never allowed as indentation.
  • Comments begin with the number sign (#), can start anywhere on a line and continue until the end of the line. Comments must be separated from other tokens by white space characters. 
  • Strings (scalars) are ordinarily unquoted, but may be enclosed in double-quotes ("), or single-quotes (').

For a complete reference of the YAML syntax please check up http://yaml.org/ or http://www.yaml.org/refcard.html.

Lists and Maps

Magnolia YAML files are always combinations of YAML maps and lists. (This said, make sure you understand the concept of these two structures.)

Lists

Members of a list are lines beginning at the same indentation level starting with leading hyphen (dash) and at least one space ( ). The number of spaces after the leading hyphen must be the same for all list members.

# A list of food
- Sandwich
- Pizza
- Burrito
- Chocolate cake

Maps

Maps (also known as dictionaries in YAML) are represented in a simple key: value  form (the colon must be followed by a space):

# An employee record
name: John Doe
job: Developer
skill: Beginner 

Combination of Map and List

Let's combine maps and a lists. (It is a common use case in Magnolia YAML files.)

---
# An employee record
name: John Doe
job: Developer
skill: Beginner
employed: True
food:
    - Sandwich
    - Pizza
    - Burrito
    - Chocolate cake
drinks: [coke, beer, water, milk] # drinks is another example of another notation of a list
languages:
    groovy: Beginner
    java: Beginner
    freeMarker: Expert

Note:

  • The "root" structure of this file is a map.
  • The value of the map entries with the keys food and drinks are lists
  • The value of the map entry with the key languages again is a map

YAML in the context of Magnolia

YAML data is transformed to definition classes

YAML files are meant to configure or define "items" like apps, templates and dialogs. In a running system the data written in YAML is represented by a Java Bean. Below you find a table of Magnolia YAML files and their corresponding Magnolia classes (usually called definition or description class). 

ItemYAML fileCorresponding definition class*
DialogmyDialog.yaml
TemplatemyTemplate.yaml
AppmyApp.yaml

* You also can use custom definition classes (which usually extend the classes mentioned above). In that case you must provide the class as an attribute in the YAML file.

Example: Using custom definition classes

Imagine you have your own template definition class com.example.magnolia.templates.CustomTemplateDefinition

package com.example.magnolia.templates;
 
public interface CustomTemplateDefinition extends TemplateDefinition{
	String getBodyClass;
}

The corresponding YAML file for this template definition would look like this:

class: com.example.magnolia.templates.CustomTemplateDefinition
renderType: freemarker
templateScript: /example-project/templates/pages/defaultTemplate.ftl
title: Default template
visible: true     
bodyClass: default-layout       

YAML data is transformed by Map2BeanTransformer. Magnolia uses snakeyaml to parse data.

The name property

A definition class often has a property name.

public class FooBarDefinition {
    private String name;
    private boolean abc;
    private boolean xyz;

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public boolean getAbc() { return abc; }
    public void setAbc(boolean abc) { this.abc = abc; }
    public boolean getXyz() { return xyz; }
    public void setXyz(boolean xyz) { this.xyz = xyz; }
}

Imagine fooBar is an item within a YAML configuration file.

If fooBar is an entry within a map, the key can be used as the value for the property name. (However it can be overridden.)

employee:
  skills:
  	programming:
      java: good
      ftl: excellent
    miscellaneous: # here the item miscellaneous is a map
      musical: False
      fooBar:
        # name: offBar (the property 'name' can be set. If not, the key is used!)
        abc: True
        xyz: False

If fooBar is an item within a list, the name property must be set explicitly.

employee:
  skills:
    programming:
      java: good
      ftl: excellent
    miscellaneous: # here the item miscellaneous is a list
      - musical: False
      - fooBar
          name: fooBar
          abc: True
          xyz: True

Differences between YAML file and JCR node based configuration data

YAML configuration has some benefits and disadvantages over JCR node based configuration. See Configuring in JCR vs YAML.

Include mechanism 

Instead of adding a list item or a value of a map entry you can include a file.

form:
  label: Example page properties
  tabs:
    - name: tabText
      label: Texts and categories
      fields:
        - name: title
          class: info.magnolia.ui.form.field.definition.TextFieldDefinition
          label: Title and categories
          i18n: true
        # an include within a list, here adding a field
        - !include /documentation-examples-templates/dialogs/common/categorization-field.yaml
# an include within a map, here adding a tab
actions: !include /documentation-examples-templates/dialogs/common/actions-block.yaml
   

The !include directive is Magnolia specific. The ! is a special character in YAML for a non-specific tag, see http://www.yaml.org/refcard.html

Path conventions:

  • Absolute path starting with /<module-name>. (This directory within a Maven-based module would be src/main/resources/<module-name>. See Module structure).
  • Same kind of definitions only. The file which includes and the file to be included must be of the same type.
    • (tick) file /my-module/dialogs/mydialog.yaml can include /my-module/dialogs/common-actions.yaml 
    • (tick) file /my-module/dialogs/mydialog.yaml can include /mte/dialogs/default-actions.yaml 
    • (error) file /my-module/dialogs/mydialog.yaml cannot include /my-module/apps/actions.yaml 

Exporting JCR configuration to YAML

You can export JCR data from the configuration workspace to YAML. This is useful for moving from repository-based configuration to file-based configuration.

YAML export is available for all definition items that can be configured in YAML:

Also see Downloading YAML definition.

Resources

Editors supporting YAML syntax

Online YAML parser