edwindj / whisker

{{mustache}} for R

Home Page:https://mustache.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Option to check parameters exist

danhalligan opened this issue · 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

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.

Will check ASAP

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)

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

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

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

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!

Edwin,

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

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