Conditional Component Inflation (APL for Audio)
Conditional component inflation determines which components to render in an APL document. For example, the mainTemplate property of an APL document accepts an array of components in the items property, but renders a single component. Conditional component inflation determines which one to render. The component inflation algorithms enable lightweight conditional expressions using arrays of components, the when property in primitive components, and data-binding.
Speech component, Alexa converts the text to speech and generates an audio clip to play for the user.Conditional inflation scenarios
Conditional inflation works differently depending on the scenario:
- Single child – Alexa renders a single component from an array of possible components.
- Simple array – Alexa renders a subset of components from an array of components.
- Data array – Alexa uses a data array to render a set of components.
The remaining sections provide more details about each of these scenarios.
Single child inflation
With single child inflation, Alexa renders at most one child component from an array of components. For example, the mainTemplate in an APL document might have multiple components in the items array, but Alexa renders one single component from the array.
|
Used by |
Components that accept an array of components in
|
|
Algorithm |
Given an array of components, evaluate the |
|
Data-binding |
By default, no additional properties are bound for single child inflation. For the |
The following example shows a Selector component. Selector renders a single child component, but takes an array of components in the items property.
{
"type": "Selector",
"items": [
{
"when": "${payload.myData.userDefinedList.length > 1}",
"type": "Speech",
"contentType": "PlainText",
"content": "Say this when userDefinedList has more than one item."
},
{
"type": "Speech",
"contentType": "PlainText",
"content": "Say this when userDefinedList has one item or no items."
}
]
}
Alexa renders the first Speech component when the array defined by payload.myData.userDefinedList contains more than one one item. Alexa renders the second Speech component when the array has zero items or one item. The when property defaults to true when not specified, so in this example, the second Speech always renders if the first component is skipped.
For example, with the following data source, the Selector creates an audio clip with the speech "Say this when userDefinedList has more than one item."
{
"myData": {
"userDefinedList": [
"item 1",
"item 2",
"item 3"
]
}
}
The following alternate data source creates an audio clip with the speech "Say this when userDefinedList has one item or no items."
{
"myData": {
"userDefinedList": [
"item 1"
]
}
}
Simple array inflation
With simple array inflation, Alexa renders a subset of components from an array of components. For example a Sequencer component accepts an array of components in the items property. Alexa renders all the items in the array, or a subset of items.
Use a simple array of items when you have a collection of heterogeneous items to render in a multiple-child component.
|
Used by |
Multiple-child components when the |
|
Algorithm |
The |
|
Data-binding |
The |
The following example shows a Sequencer component, which renders multiple components one after another. In this example, the data property is omitted, so the component uses the simple array approach to evaluate all the components in items and choose the subset to render.
{
"type": "Sequencer",
"items": [
{
"type": "Speech",
"content": "hello world."
},
{
"type": "Speech",
"when": "${environment.aplVersion}",
"content": "Check your screen for the daily greeting!"
}
]
}
The environment.aplVersion property returns the version of APL supported by the device. It returns null on a device that doesn't have a screen or otherwise doesn't support APL for screen devices. Therefore, in this example:
- When the device has a screen, the
Sequencerrenders both components and the user hears "Hello world! Check your screen for the daily greeting!" - When the device doesn't have a screen, the
Sequencerrenders the first component and ignores the second, so the user hears "Hello world!"
Data array inflation
With data array inflation, Alexa uses an array of data to inflate a set of components. This method is available for multiple-child components when the data property is set.
Use the data array approach when you have an array of data that you want to concatenate together. You define the set of components to render. Alexa renders that set of components for each item in data.
|
Used by |
Multiple-child components when the |
|
Algorithm |
Each element in the |
|
Data-binding |
The |
The following example illustrates a Sequencer component with the data property set to an array of strings representing the steps of a recipe. The component then has a single component in item that uses the data variable to render content from the data array.
{
"type": "Sequencer",
"item": {
"type": "Speech",
"content": "${index+1}. ${data}"
},
"data": "${payload.recipeData.steps}"
}
This example uses the following data source to provide the array of data to the data property.
{
"recipeData": {
"steps": [
"Preheat the oven to 350 degrees F.",
"Blend the butter and sugar in a mixer until smooth.",
"Add one egg at a time to the butter-sugar mixture and beat until smooth.",
"In a separate bowl, combine...."
]
}
}
Alexa evaluates the single Speech component one time for each string in data. If the when property evaluates to true, Alexa renders the component. In this example, the when property defaults to true, so Alexa renders the component four times, one time for each string in data. The user hears all four strings, one after the other.
A more complicated example uses when statements and nests Sequencer components to inflate a heterogeneous set of data.
{
"type": "Sequencer",
"items": [
{
"when": "${data.opening}",
"type": "Speech",
"content": "${data.opening}"
},
{
"type": "Sequencer",
"items": [
{
"type": "Speech",
"content": "${data.speech1}"
},
{
"type": "Speech",
"content": "${data.speech2}"
}
]
}
],
"data": "${payload.speechData}"
}
This example uses the following data source to provide the array of data to the data property.
{
"speechData": [
{
"opening": "opening speech"
},
{
"speech1": "first thing",
"speech2": "second thing"
}
]
}
Alexa evaluates the Sequencer one time for each of the two items in data:
- The first item is object with an
openingproperty. Alexa selects the firstSpeechcomponent since${data.opening}istrue. The nestedSequencerwith the twoSpeechcomponents is effectively ignored because thedata.speech1anddata.speech2values are both empty. - The second item is an object with
speech1andspeech2properties. Alexa ignores the firstSpeechcomponent since${data.opening}isfalse. The nestedSequenceris inflated using the simple array of child components, so Alexa renders bothSpeechcomponents.
The end result is the speech "opening speech first thing second thing".
Related topics
Last updated: Nov 28, 2023