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

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

Open
ldecicco-USGS opened this issue May 6, 2021 · 2 comments
Open

Comments

@ldecicco-USGS
Copy link

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
Copy link
Author

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
Copy link
Author

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 ldecicco-USGS reopened this May 6, 2021
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

1 participant