String templates within string templates?

#1

I’m trying to make a skill for Amazon Alexa where she reads an array, but I’ve hit a roadblock. I want to implement a string template that looks like this: {{data.{{data.number}}.task.title}} However, I tried everything, but it won’t work! How can I make this happen?

#2

Given a payload of …

{
  "indexToLookUp": 1,
  "data": [
    {
      "task": { 
        "title": "Title Zero"
      }
    },
    {
      "task": {
        "title": "Title One"
      }
    }
  ]
}

You can return “Title One” using the following string template:

{{#with (lookup data indexToLookUp)}}{{task.title}}{{/with}}

There are a few things going on here:

  1. The with block helper shifts the string template’s context from the full payload object down to an object of our choosing for anything within the block.
  2. The lookup helper allows us to return an array item by index. It takes two arguments: an array and an index. We pass it data from our payload and indexToLookUp, which is a number that is also on the payload.
  3. Wrapping the lookup helper in parentheses treats it as a subexpression, the result of which is what we pass as the argument to the “with” block helper.
  4. Now, inside the “with” block helper, we can access the deep object property task.title as we normally would.
#3

Unfortunately handlebars isn’t very helpful when accessing data like this. An alternative approach could be to use a function node to move the data to a more accessible place on the payload.

var index = payload.data.number;
payload.data.titleToRead = payload.data[index].task.title;

Then you can access it using the template:

{{ data.titleToRead }}}