edwindj / whisker

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

Option to check parameters exist #15

Open danhalligan opened 10 years ago

danhalligan commented 10 years ago

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

edwindj commented 10 years ago

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.

danhalligan commented 10 years ago

Will check ASAP

danhalligan commented 10 years ago

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)

edwindj commented 10 years ago

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

ctbrown commented 8 years ago

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

edwindj commented 8 years ago

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

danhalligan commented 8 years ago

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!

ctbrown commented 8 years ago

Edwin,

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

RoyalTS commented 4 years ago

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