Magnolia 5.4 reached end of life on November 15, 2018. This branch is no longer supported, see End-of-life policy.
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. Magnolia 5.4+
#
), 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. "
), or single-quotes ('
).For a complete reference of the YAML syntax please check up http://yaml.org/ or http://www.yaml.org/refcard.html.
Magnolia YAML files are always combinations of YAML maps and lists. (This said, make sure you understand the concept of these two structures.)
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 (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
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:
food
and drinks
are listslanguages
again is a mapYAML files are meant to configure or define "items" like app, 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).
Item | YAML file | Corresponding definition class* |
---|---|---|
Dialog | myDialog.yaml |
DialogDefinition
|
Template | myTemplate.yaml |
TemplateDefinition
|
App | myApp.yaml |
AppDescriptor
|
* 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.
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.
YAML configuration has some benefits and disadvantages over JCR node based configuration. See Configuring in JCR vs YAML.
Use the Magnolia !include
directive to add a reusable YAML chunk. Include a fragment on a sub-level of your definition. Reference the file you include by its resource path starting with /<module-name>/
.
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/includes/categorization-field.yaml # an include within a map, here adding a tab actions: !include /documentation-examples-templates/includes/default-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.
If you have created the includable fragment after the creation of the file which includes it, save the latter again to force the YAML parser to properly register the definition. /<module-name>/includes
is the recommended directory for YAML fragment files. Avoid adding the YAML fragment files into the following directories: apps
, blocks
, decorations
, dialogs
, i18n
, messageViews
, renderers
, templates
and virtualUriMappings
.
With Magnolia 5.5.6+, the
!include
directive has been extended to provide more functionalities. (See YAML inherit and include).
You can export JCR data from the YAML export is only available for definition items: The Export to YAML action is disabled for other items. When using YAML files originating from export, you might have to change the file name if you want to use it within a module; for instance the name of the YAML file defining an app must reflect the app name with the pattern config
workspace to YAML. This is useful for moving from repository-based configuration to file-based configuration.<app-name>.yaml
.