edwindj / whisker

{{mustache}} for R
https://mustache.github.io
213 stars 19 forks source link

impossible to use named sections when key is unknown #30

Open bjoernhommel opened 4 years ago

bjoernhommel commented 4 years ago

Thanks for a great package. R really needs a good templating engine.

If the names of hash$people are unknown, there appears to be now way of accessing the data in it's children.

library(whisker)

hash <- list(
  people = list(
    a = list(
      first = "john",
      last = "johnsson"
    ),
    b = list(
      first = "peter",
      last = "petersson"
    ),
    c = list(
      first = "steve",
      last = "stevesson"
    )
  ),
  stuff = c(1:5)
)

template <- "
{{#people}}
<h1>Hello {{first}}</h1>
{{/people}}
"

result <- whisker::whisker.render(template = template, data = hash)
print(HTML(result))

Link to stackoverflow.