Skip to content
Jair Andres Diaz Puentes edited this page Mar 15, 2018 · 1 revision

$root

Sometimes you need to refer to the root data object while iterating through an #each loop.

In this case you can use a special keyword named $root.

var template = {
  "{{#each posts}}": [
    "{{content}}",
    "{{$root.users[user_id]}}"
  ]
}
var data = {
  users: ["Alice", "Bob", "Carol"],
  posts: [{
    content: "Show me the money",
    user_id: 1
  }, {
    content: "hello world",
    user_id: 0
  }, {
    content: "what is the meaning of life?",
    user_id: 2
  }]
}

Select and transform

ST.select(template)
    .transform(data)
    .root();

Get the result

[
  ["Show me the money", "Bob"],
  ["hello world", "Alice"],
  ["what is the meaning of life?", "Carol"]
]
Clone this wiki locally