edwindj / whisker

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

Feature request: function to return data frame of keys in template #33

Open zkamvar opened 3 years ago

zkamvar commented 3 years ago

It would be nice to have a function that generates a data frame of keys for a given template for programmatic generation of input data. I'm thinking something along the lines of:

template <- 
    "{{#dat}}
    count: {{count}}, spray: {{spray}}\n
    {{/dat}}" 
whisker::getKeys(template)
#>   rawkey first last   key type
#> 1   #dat     1    8   dat    #
#> 2  count    20   28 count     
#> 3  spray    38   46 spray     
#> 4   /dat    49   56   dat    /

FWIW, the functionality in {whisker} already exists, it just needs to be exposed:

template <- 
    "{{#dat}}
    count: {{count}}, spray: {{spray}}\n
    {{/dat}}" 

f <- whisker:::parseTemplate(template)
get("key", envir = environment(f))
#>   rawkey first last   key type
#> 1   #dat     1    8   dat    #
#> 2  count    20   28 count     
#> 3  spray    38   46 spray     
#> 4   /dat    49   56   dat    /

Created on 2020-09-10 by the reprex package (v0.3.0)