whisker icon indicating copy to clipboard operation
whisker copied to clipboard

Is there a way to get the parent variable in a nested loop?

Open ldecicco-USGS opened this issue 3 years ago • 2 comments

I'm exploring how to set up a nested loop. Here's my template and call to whisker.render:

temp_string <- "
{{#i}}
# {{.}}
{{#j}}
## {{.}}
key = paste({{../}}, {{.}})
{{/j}}
{{/i}}"

template_contents <- strsplit(whisker::whisker.render(temp_string,
                                                      data = list(j = 1:2,
                                                                  i = letters[1:3])), "\n")[[1]]

Which results in:

# a
## 1
key = paste(, 1)
## 2
key = paste(, 2)
# b
## 1
key = paste(, 1)
## 2
key = paste(, 2)
# c
## 1
key = paste(, 1)
## 2
key = paste(, 2)

ALMOST what I need! Is there any way to get the {{../}} to pull the "parent" i?

ldecicco-USGS avatar May 06 '21 20:05 ldecicco-USGS

I didn't figure it out, but I figured out a way to do it with the list stuff:

temp_string2 <- "
{{#section}}
# {{name}}
{{#i}}
## {{.}}
key = paste({{name}}, {{.}})
{{/i}}
{{/section}}
"
template_contents <- strsplit(whisker::whisker.render(temp_string2,
                                 data =
           list(section = list(list(name = "1",
                              i = letters[1:3]),
                          list(name = "2",
                               i = letters[1:3])))), "\n")[[1]]

which produced:

# 1
## a
key = paste(1, a)
## b
key = paste(1, b)
## c
key = paste(1, c)
# 2
## a
key = paste(2, a)
## b
key = paste(2, b)
## c
key = paste(2, c)

ldecicco-USGS avatar May 06 '21 21:05 ldecicco-USGS

OK, my brain is absolutely breaking trying to taking this one layer deeper....being able to reference the "parent" (one level up) would help me a ton. Is it possible in whisker?

ldecicco-USGS avatar May 06 '21 23:05 ldecicco-USGS