Jade: a clean, whitespace-sensitive template language for writing HTML

February 19, 2015
By

(This article was first published on OpenCPU, and kindly contributed to R-bloggers)

opencpu logo

Jade is a high performance template engine heavily influenced by Haml. It is designed for writing HTML pages using a concise, modern syntax without the verbosity of old fasioned XML-like tags that we all want to forget about. The new rjade package implements convenient bindings from R to this popular JavaScript library.

An example template

Below an example of a Jade template, taken from the jade homepage. Notice that the notation of tags, classes and id’s much resembles CSS selectors. The template also includes one variable called youAreUsingJade, which we can use to control the rendering output.

doctype html
html(lang="en")
  head
    title= pageTitle
    script(type='text/javascript').
      if (foo) {
         bar(1 + 5)
      }
  body
    h1 Jade - node template engine
    #container.col
      if youAreUsingJade
        p You are amazing
      else
        p Get on it!
      p.
        Jade is a terse and simple
        templating language with a
        strong focus on performance
        and powerful features.

Converting a template to HTML text involves two steps. The first step compiles the template with some formatting options into a closure. The binding for this is implemented in jade_compile.

# Compile a Jade template in R
library(rjade)
text <- readLines(system.file("examples/test.jade", package = "rjade"))
tpl <- jade_compile(text, pretty = TRUE)

The second step calls the closure with optionally some local variables to render the output to HTML.

# Render the template
tpl()

The output looks like this:


 lang="en">
  
    
    
  
  
    

Jade - node template engine

id="container" class="col">

Get on it!

Jade is a terse and simple templating language with a strong focus on performance and powerful features.

Note how the HTML output changes when setting local variables:

tpl(youAreUsingJade = TRUE)

 lang="en">
  
    
    
  
  
    

Jade - node template engine

id="container" class="col">

You are amazing

Jade is a terse and simple templating language with a strong focus on performance and powerful features.

That’s it. Hover over to the jade website to learn about the full power of this amazing templating language.

To leave a comment for the author, please follow the link and comment on their blog: OpenCPU.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Recent popular posts

Most visited articles of the week

  1. How to write the first for loop in R
  2. Installing R packages
  3. Using apply, sapply, lapply in R
  4. R – Sorting a data frame by the contents of a column
  5. How to perform a Logistic Regression in R
  6. How to Make a Histogram with Basic R
  7. Tutorials for learning R
  8. How to Make a Histogram with ggplot2
  9. In-depth introduction to machine learning in 15 hours of expert videos

Sponsors

RSS Jobs for R users

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)