Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

whisker does not support #sections with non empty lists #22

Open
ctbrown opened this issue Mar 31, 2016 · 1 comment
Open

whisker does not support #sections with non empty lists #22

ctbrown opened this issue Mar 31, 2016 · 1 comment

Comments

@ctbrown
Copy link

ctbrown commented Mar 31, 2016

While whisker supports sections, it does not appear to support sections with non-empty lists as described in https://mustache.github.io/mustache.5.html:

Template:

{{#repo}}
  <b>{{name}}</b>
{{/repo}}

Hash:

{
  "repo": [
    { "name": "resque" },
    { "name": "hub" },
    { "name": "rip" }
  ]
}

Output:

<b>resque</b>
<b>hub</b>
<b>rip</b>

Using JSON

This closely follows the mustache documentation by usingjsonlite to convert the hash to an R object. The R object is a list with a data frame of 3 rows.

library(jsonlite)
library(whisker)

tmpl <- 
  "{{#repo}}
  <b>{{name}}</b>
  {{/repo}}"

hash <- 
  '{
    "repo": [
      { "name": "resque" },
      { "name": "hub" },
      { "name": "rip" }
    ]
  }'

data <- fromJSON(hash)  # list of data.frame not list of list


whisker.render(tmpl,data)   

This does not conform to the specification is not correct as in collapses the name column rather than returning three results. According to the specification, I would expect this result:

c("<b>resque</b>","<b>hub</b>","<b>rip</b>")

Using list-of-lists

Using a list of lists does not work either:

data <- list( repo = list( name="resque", name="hub", name="rip") )
whisker.render(tmpl,data) 
# [1] "<b>resque</b>"
@pmarchand1
Copy link

You need one more level of nesting.
data <- list(repo = list(list(name = "resque"), list(name = "hub"), list (name = "rip")))

The fromJSON call returns the correct format as long as simplifyVector is FALSE i.e.
data <- fromJSON(hash, simplifyVector = FALSE)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants