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

Option to check parameters exist #15

Open
danhalligan opened this issue Jun 10, 2014 · 9 comments
Open

Option to check parameters exist #15

danhalligan opened this issue Jun 10, 2014 · 9 comments

Comments

@danhalligan
Copy link

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 added a commit that referenced this issue Jun 10, 2014
@edwindj
Copy link
Owner

edwindj commented Jun 10, 2014

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

Will check ASAP

@danhalligan
Copy link
Author

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

edwindj commented Jun 11, 2014

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

@ctbrown
Copy link

ctbrown commented Apr 5, 2016

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

@edwindj
Copy link
Owner

edwindj commented Apr 5, 2016

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

@danhalligan
Copy link
Author

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

ctbrown commented May 3, 2016

Edwin,

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

@RoyalTS
Copy link

RoyalTS commented Nov 19, 2019

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

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

4 participants