Razor Leaf is an HTML template engine for Node.js.
-
automatic escaping
-
template inheritance
-
recursive macros
-
conditional attributes and classes
-
explicit whitespace only to make it clear when it affects the content
-
no dependencies*
-
unrestricted JavaScript expressions and inline code; no sandboxes to work around
* This might change.
example.rl
:
doctype html
head
meta charset: "utf-8"
meta name: "viewport" content: "initial-scale=1"
title "Example"
body
h1 "Hello, world!"
do const { left, right } = data;
p "#{left} × #{right} = #{left * right}"
example.js
:
const DirectoryLoader = require('razorleaf/directory-loader');
const templateLoader = new DirectoryLoader(__dirname);
const template = templateLoader.load('example');
console.log(
template({
left: 9,
right: 12,
})
);
Output:
9 × 12 = 108
Elements are defined by their names only; no other special character is necessary.
p
<p></p>
Void elements are recognized automatically.
meta
<meta>
Strings are double-quoted and escaped for use in HTML as needed. Backslash escape codes can be used as in JavaScript. No whitespace is added around strings.
"--> A string <--\n" "A string containing \"double-quotes\""
--> A string <--
A string containing "double-quotes"
Strings can also contain interpolated sections, delimited by #{
and }
.
#{
can be escaped with a leading backslash; }
doesn’t require escaping.
"#{6 * 7}"
42
If an exclamation mark precedes the string, it and any of its interpolated sections will not be escaped.
!"<!-- A significant comment -->"
<!-- A significant comment -->
Attributes are marked up using the syntax name: "value"
, where "value"
is a string as described above. Use an empty string for boolean attributes.
meta charset: "utf-8"
<meta charset="utf-8">
If a boolean attribute is conditional, it can be written in shorthand:
input
name: "#{field.name}"
disabled: if field.disabled
equivalent to:
input
name: "#{field.name}"
if field.disabled
disabled: ""
Classes are marked up with a leading period, as in .class
.
fieldset .upload-meta
input.required
<fieldset class="upload-meta"><input class="required"></fieldset>
Hierarchy in Razor Leaf is defined using indentation. For example:
doctype
html
head
meta charset: "utf-8"
title "Example"
link
rel: "stylesheet"
type: "text/css"
href: "stylesheets/example.css"
body
p id: "introduction"
"This template is a brief example of hierarchy."
<!DOCTYPE html><html><head><meta charset=utf-8><title>Example</title><link rel=stylesheet type=text/css href=stylesheets/example.css></head><body><p id=introduction>This template is a brief example of hierarchy.</p></body></html>
Content found after an element on the same line will also be considered that element’s content.
Comments begin with #
and continue to the end of the line. They do not affect
the rendered HTML.
Code blocks begin with do
and treats all of their content as JavaScript.
do
const compareKeys = (a, b) =>
a.key < b.key ? -1 :
a.key > b.key ? 1 :
0;
const sorted = (array, by) =>
array
.map(value => ({key: by(value), value}))
.sort(compareKeys)
.map(({value}) => value);
let characterCount = 0;
for post of sorted(posts, post => post.title)
post-detail(post)
do characterCount += post.content.length;
Some names define special blocks. These are:
doctype
: Inserts<!DOCTYPE html>
.if (condition)
: Includes its content only ifcondition
is met.elif (condition)
: Can immediately follow anif
or anelif
.else
: Can immediately follow anif
or anelif
.for (identifier) of (collection)
: Includes its content for each element of the array or array-like objectcollection
.for (identifier), (index) of (collection)
: Allows the index variable in afor
loop to be named.include (name)
: Loads and includes another template.extends (name)
: Loads another template and replaces its blocks. A template that extends another template cannot have any content outside of block actions.block (name)
: Defines a replaceable block.replace (name)
: Replaces a block.append (name)
: Appends to a block.do
: See Code.
Creates a loader that maps template names to files with the .rl
extension
in the directory located at root
.
Returns a template object loaded from the root directory.
Compiles a template string into a function. The compiled function takes
one argument, data
, which can be used (under that name) in the template.
load(name)
: A function that returns a parsed template represented byname
. This is filled automatically by most loaders.globals
: An object representing the global variables that should be made available to the template.