Skip to content
{{mustache}} for R
Branch: master
Clone or download
Latest commit 7d3dcc4 Jul 31, 2015
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
R Updated documentation for disabling html escaping. (issue #16)(Thanks… Jun 18, 2014
examples Updated documentation for disabling html escaping. (issue #16)(Thanks… Jun 18, 2014
inst improved test, so it confirms with mustache Apr 13, 2014
man Updated documentation for disabling html escaping. (issue #16)(Thanks… Jun 18, 2014
.Rbuildignore adding badges Jul 31, 2015
.Rprofile Added .Rprofile (useful for testing) Aug 30, 2012
.gitignore update gitignore Aug 30, 2012
.travis.yml
DESCRIPTION Fixed issue #12: inconsistent treatment of new lines (Thanks to @danh… Apr 13, 2014
NAMESPACE
NEWS Updated documentation for disabling html escaping. (issue #16)(Thanks… Jun 18, 2014
README.md Update README.md Jul 31, 2015
README.rmd added extra example in whisker README (thanks to @timrobertson100), i… May 15, 2014
appveyor.yml adding badges Jul 31, 2015
whisker.Rproj Support R variables with . and data.frames with $, added an option Mar 4, 2014

README.md

version downloads Build Status Build status

Whisker

Whisker is a {{Mustache}} implementation in R confirming 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. It makes it very easy to write a web application in R using Mustache templates which could also be re-used for client-side rendering with "Mustache.js".

Mustache (and therefore whisker) takes a simple, but different, approach to templating compared to most templating engines. Most templating libraries, such as Sweave, knitr and brew, allow the user to mix programming code and text throughout the template. This is powerful, but ties your template directly to a programming language and 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.

Mustache syntax

The syntax of Mustache templates is described in http://mustache.github.com/mustache.5.html How the mustache template are used with whisker can be found in the whisker documentation, and below.

Mustache specification

Whisker conforms to the Mustache 1.1 specificaton except for delimiter switching and lambdas. We expect that these will be implented shortly.

Installation

To install whisker use the following statement in your R console

install.packages("whisker")

The latest whisker version is not yet available on CRAN, but can be installed from github:

library(devtools)

# dev_mode()
install_github("whisker", "edwindj")

Usage

whisker.render accepts a character template and a list or environment containing data to render:

library(whisker)
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
            )

text <- whisker.render(template, data)
cat(text)
## Hello Chris
## You have just won $10000!
## Well, $6000, after taxes.

Or using a text file

library(whisker)

template <- readLines("./template.html")
data <- list( name = "Chris"
            , value = 10000
            , taxed_value = 10000 - (10000 * 0.4)
            , in_ca = TRUE
            )

writeLines(whisker.render(template, data), "./output.html")

Note

By default whisker applies html escaping on the generated text. To prevent this use {{{variable}}} (triple) in stead of {{variable}}.

template <- 
"I'm escaped: {{name}}
And I'm not: {{{name}}}"

data <- list( name = '<My Name="Nescio">')
whisker.render(template, data)

Generates:

I'm escaped: &lt;My Name=&quot;Nescio&quot;&gt;
And I'm not: <My Name="Nescio">
You can’t perform that action at this time.