Closed francisbarton closed 4 years ago
Thanks for trying whisker! One way to render what you want with whisker is:
library(whisker)
library(tibble)
df <- tibble(
name = c("Adam", "Barbara", "Chris"),
home = c("Aylesbury", "Blackburn", "Coxwold"),
url = c("adamsapples.co.uk", "barbsberries.co.uk", "coxwoldcherries.co.uk")
)
# split a data.frame in list of rows.
person <- rowSplit(df)
# create a mustache section
templ <-
"{{#person}}
<li>
<a href=\"{{url}}\">{{name}} from {{home}}</a>
</li>
{{/person}}"
txt <- whisker::whisker.render(templ, list(person = person))
cat(txt)
## <li>
## <a href="adamsapples.co.uk">Adam from Aylesbury</a>
## </li>
## <li>
## <a href="barbsberries.co.uk">Barbara from Blackburn</a>
## </li>
## <li>
## <a href="coxwoldcherries.co.uk">Chris from Coxwold</a>
## </li>
Best,
Edwin
That's excellent - thank you Edwin. I want to come back to this and try to understand it better (I'm wondering whether rowwise()
might be relevant) but for now I'm just going to post this to says thanks for the explanation/solution.
I am new to
whisker
but my assumption with a template is that its main purpose is to be used when iterating over a vector or list. (If you just want output of length 1 you'd just write the output directly).But I can't work out how to feed a set of lists (eg a tibble) as the environment for
whisker
- the documentation isn't clear enough for me. Here's a reprex of what I'm trying. I've usedpurrr::pmap
to show what I want, and then tried to achieve the equivalent withwhisker
.Created on 2020-03-20 by the reprex package (v0.3.0)