Title: | {{mustache}} for R, logicless templating |
---|---|
Description: | Implements 'Mustache' logicless templating. |
Authors: | Edwin de Jonge [aut, cre] |
Maintainer: | Edwin de Jonge <[email protected]> |
License: | GPL-3 |
Version: | 0.5 |
Built: | 2024-11-10 05:45:12 UTC |
Source: | https://github.com/edwindj/whisker |
Whisker is a templating engine for R conforming to the Mustache specification. Mustache is a logicless templating language, meaning that no programming source code can be used in your templates. This may seem very limited, but Mustache is nonetheless powerful and has the advantage of being able to be used unaltered in many programming languages. For example it make it very easy to write a web application in R using Mustache templates and where the browser can template using javascript's "Mustache.js"
Mustache (and therefore whisker
) takes a simple but different approach to templating compared to
most templating engines. Most templating libraries for example Sweave
and brew
allow the user
to mix programming code and text throughout the template. This is powerful, but ties a template directly
to a programming language. Furthermore that approach makes it difficult to seperate programming code
from templating code.
Whisker on the other hand, takes a Mustache template and uses the variables of the current environment (or the
supplied list
) to fill in the variables.
template <- 'Hello {{name}} You have just won ${{value}}! {{#in_ca}} Well, ${{taxed_value}}, after taxes. {{/in_ca}}' data <- list( name = "Chris" , value = 10000 , taxed_value = 10000 - (10000 * 0.4) , in_ca = TRUE ) whisker.render(template, data) base <- '<h2>Names</h2> {{#names}} {{> user}} {{/names}}' user <- '<strong>{{name}}</strong>' names <- list(list(name="Alice"), list(name="Bob")) whisker.render(base, partials=list(user=user))
template <- 'Hello {{name}} You have just won ${{value}}! {{#in_ca}} Well, ${{taxed_value}}, after taxes. {{/in_ca}}' data <- list( name = "Chris" , value = 10000 , taxed_value = 10000 - (10000 * 0.4) , in_ca = TRUE ) whisker.render(template, data) base <- '<h2>Names</h2> {{#names}} {{> user}} {{/names}}' user <- '<strong>{{name}}</strong>' names <- list(list(name="Alice"), list(name="Bob")) whisker.render(base, partials=list(user=user))
In some case it is useful to iterate over a named list
or vector
iteratelist
will create a new unnamed list
with name value members:
each item will be a list where 'name' is the corresponding name and 'value' is the original
value in list x
.
iteratelist(x, name = "name", value = "value")
iteratelist(x, name = "name", value = "value")
x |
|
name |
|
value |
|
unnamed list
with name value lists
# create an iteration list from a named vector x <- c(a=1, b=2) iteratelist(x) # iterate over the members of a list x <- list(name="John", age="30", gender="male") iteratelist(x, name="variable") # iterate over an unnamed vector values <- c(1,2,3,4) template <- '{{#values}} * Value: {{.}} {{/values}}' whisker.render(template) #or values <- iteratelist(values, value="count") template <- '{{#values}} * Value: {{count}} {{/values}}' whisker.render(template)
# create an iteration list from a named vector x <- c(a=1, b=2) iteratelist(x) # iterate over the members of a list x <- list(name="John", age="30", gender="male") iteratelist(x, name="variable") # iterate over an unnamed vector values <- c(1,2,3,4) template <- '{{#values}} * Value: {{.}} {{/values}}' whisker.render(template) #or values <- iteratelist(values, value="count") template <- '{{#values}} * Value: {{count}} {{/values}}' whisker.render(template)
Utility function for splitting a data.frame into rows. In a whisker template it can be useful to iterate over the rows of a data.frame or matrix. For example rendering a table in HTML.
rowSplit(x, ...)
rowSplit(x, ...)
x |
|
... |
other options will be passed onto |
dat <- head(InsectSprays) dat <- unname(rowSplit(dat)) template <- "{{#dat}} count: {{count}}, spray: {{spray}}\n {{/dat}}" whisker.render(template)
dat <- head(InsectSprays) dat <- unname(rowSplit(dat)) template <- "{{#dat}} count: {{count}}, spray: {{spray}}\n {{/dat}}" whisker.render(template)
This method is called for normal mustache keys
whisker.escape(x)
whisker.escape(x)
x |
|
HTML escaped character
Logicless templating
whisker.render( template, data = parent.frame(), partials = list(), debug = FALSE, strict = TRUE )
whisker.render( template, data = parent.frame(), partials = list(), debug = FALSE, strict = TRUE )
template |
|
data |
named |
partials |
named |
debug |
Used for debugging purposes, likely to disappear |
strict |
|
character
with rendered template
By default whisker applies html escaping on the generated text. To prevent this use {{{variable}}} (triple) in stead of {{variable}}.
template <- "Hello {{place}}!" place <- "World" whisker.render(template) # to prevent html escaping use triple {{{}}} template <- "I'm escaped: {{name}} And I'm not: {{{name}}}" data <- list( name = '<My Name="Nescio&">') whisker.render(template, data) # I'm escaped: <My Name="Nescio&"> # And I'm not: <My Name="Nescio&">
template <- "Hello {{place}}!" place <- "World" whisker.render(template) # to prevent html escaping use triple {{{}}} template <- "I'm escaped: {{name}} And I'm not: {{{name}}}" data <- list( name = '<My Name="Nescio&">') whisker.render(template, data) # I'm escaped: <My Name="Nescio&"> # And I'm not: <My Name="Nescio&">