whisker icon indicating copy to clipboard operation
whisker copied to clipboard

Option to check parameters exist

Open danhalligan opened this issue 10 years ago • 9 comments

I think it would be great to have the ability to turn on an option which checked to see if parameters existed, and printed a warning / died if missing, e.g. something like

 template <- "Hello {{place}}!"
 whisker.render(template, checkvars=TRUE)

produces a warning, e.g.

  Warning: variable "place" undefined in ....

Due to the possibility of nested tags, this is very hard to check externally, so something like this is hard to verify without parsing the structure of the mustache file and comparing to the structure of the R environment:

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

This is an issue as minor typos can lead to missing variables and hard to detect errors in rendered templates.

See also discussion here: http://stackoverflow.com/questions/16595621/error-safe-templating-with-brew-whisker

danhalligan avatar Jun 10 '14 15:06 danhalligan

I added a fix for this in branch "checkvars". Could you check if it works correcly? If so, I will merge it into the master branch.

edwindj avatar Jun 10 '14 18:06 edwindj

Will check ASAP

danhalligan avatar Jun 10 '14 18:06 danhalligan

Thanks for the quick fix, though at present it seems whisker.render fails to spot a missing value in a list, for example:

template <- "Hello {{name}}
You have just won {{value}} dollars!
{{#in_ca}}
Well, {{taxed_value}} dollars, after taxes.
{{/in_ca}}
{{#repo}}
  <b>{{person}}</b>
{{/repo}}"

data <- list(
    name = "Chris",
    taxed_value = 10000 - (10000 * 0.4),
    repo = list(
       list(pesron = "bob"),
       list(person = "dan"),
       list(person = "mary")
    )
)
cat(whisker.render(template, data, checkvars=TRUE))

produces:

Hello Chris
You have just won  dollars!
  <b></b>
  <b>dan</b>
  <b>mary</b>
Warning messages:
1: In tmpl(data) : Missing 'value'
2: In tmpl(data) : Missing 'in_ca'

i.e. missing the mis-spelling in line list(pesron = "bob").

One other minor point - at present the option checkvars is not in help(whisker.render)

danhalligan avatar Jun 10 '14 19:06 danhalligan

Thanks for the quick testing and your example. I will look into it later this week (bit busy coming days).

edwindj avatar Jun 11 '14 04:06 edwindj

Is this something that is likely to be fixed in the coming weeks? months? years?

ctbrown avatar Apr 05 '16 13:04 ctbrown

I'm planning to do a whisker update in two weeks and take this issue into account. Sorry for the delay...

edwindj avatar Apr 05 '16 13:04 edwindj

It would be great to see this incorporated -- it helps a lot in checking that template outputs are valid and its a bit of a pain to not be able to work from the canonical version of whisker. Glad to hear there's a new version planned!

danhalligan avatar Apr 24 '16 08:04 danhalligan

Edwin,

Any word on that whisker update? Do you accept pull requests? Do you need help pushing this out to CRAN?

ctbrown avatar May 03 '16 22:05 ctbrown

I'm greatly enjoying whisker but this feature is sorely lacking. Any news on this?

RoyalTS avatar Nov 19 '19 08:11 RoyalTS