Created: 2012-03-21 09:16
Updated: 2017-05-22 04:52
License: mit

Slab: Simple Templating

Slab is a templating engine for JavaScript with a simple syntax and an even simpler API.

How does it look like?

// slab template
var templateString = "{slab myTemplate} \
	<ul> \
		{each person in people} \
			<li>Hello {}! You are {person.age} year{person.age > 1 ? 's' : ''} old!</li> \
		{end} \
	</ul> \

// js
var templates = slab.compile(templateString);
var result = templates.myTemplate({
	people: [
		{name: 'John', age: '22'},
		{name: 'Peter', age: '31'},
		{name: 'Matthew', age: '36'},
		{name: 'Mark', age: '24'}


Template Syntax

Slab templates are declared using the slab statement, together with an identifier that's gonna be used for the template:

{slab someIdentifier}
	Hello World!

You can have multiple slab declarations in a single template:

{slab hello}
	Hello World!

{slab hi}
	Hi Universe!

You can comment your templates using a multiline comment syntax:

{slab example}
	{* This is a comment *}
	{'This will be printed out!'}

You can output the value of any expression just by writing the expression inside curly braces:

{slab example}
	{* Literals *}
	{'Strings in single quotes, not double!'}

	{* Simple variable evaluation *}

	{* Function calls *}

	{* Expressions *}
	{firstName + '' + lastName}

If you're outputting data that needs to be escaped, you can prepend a colon (:) on the output statement:

{slab example}

Slab understands both the regular JavaScript operators as well as "word-operators" in expressions:

and 	-> &&
or 		-> ||
is 		-> ==
isnt 	-> !=
eq 		-> ===
neq 	-> !==
not 	-> !

These operators can be used in Slab's branching statements:

{slab example}
	{* If statement *}
	{if firstName is 'Slab'}
		Hello {firstName}

	{* If-Else statement *}
	{if firstName is 'Slab' and lastName isnt 'Slabgle'}
		Hello {firstName}
		Hello Stranger!

	{* If-Else-If statement *}
	{if firstName eq 'Slab' or firstName neq 'SLAB'}
		Hello {firstName}
	{else if firstName isnt 'Slabble' and firstName isnt 'Slabby'}
		Hi Stranger!

For arrays, you can iterate through items using an each statment:

{slab example}
	{* Array Iteration *}
	{each collection in collections}

	{* Array Iteration with Index *}
	{each index,collection in collections}
		{index}: {collection}

Objects can be iterated using the for statement:

{slab example}
	{* Object Iteration *}
	{for person in people}

	{* Object Iteration with Key *}
	{for id,person in people}
		{id}: {}

Slab's for statement uses a hasOwnProperty guard internally to filter out inherited members. If you want to iterate through all members, use forEvery:

{slab example}
	{forEvery member in object}

Because Slab's parser looks for statements inside a single pair of curly braces, you might run into some issues with stuff you don't want to get parsed. The special keep statement can be used to escape parts of your template that you don't want to be included in parsing, such as inline scripts and styles:

{slab example}
		#someStyle {color: orange;}
		var obj = {'this': 'will not be parse'};

Slab allows you to include a template inside another template. There's no special designation for partials: all templates can be included into other templates using the include statement:

{slab list}
		{each person in people}
			{include listItem}

{slab listItem}

Compiling Templates

Slab templates can be compiled using the slab.compile function:

// A template string
var template = '{slab example}Hello {name}{endslab}!';

// Compile the template
var templates = slab.compile(template);

// Pass a data object to evaluate
templates.example({name: 'Slab'}) == 'Hello Slab!';

The slab.compile function takes a template string and returns a new SlabTemplate object. The keys of this object correspond to the identifiers you used to declare your templates, with template function values. You can then pass a data object to these functions and you'll get a string return value.

Extending Compiled Templates

If you want to override a specific template without having to change the template string directly, you can "extend" a SlabTemplate object by passing it together with the new templates to slab.compile:

// The original template
var template = '{slab example}Hello {name}{endslab}!';

// Compile the template
var templates = slab.compile(template);
templates.example({name: 'Slab'}) == 'Hello Slab!';

// A new template
template = '{slab example}Hi {name}{endslab}';
slab.compile(template, templates);
templates.example({name: 'Slab'}) == 'Hi Slab!';

Precompiling Templates

Slab uses a regexp-based parser for compiling the templates into valid JavaScript functions. This compilation is a pretty expensive process, although the resulting final functions are quite fast.

While it's alright to use slab.compile during development, it's advised that you precompile your templates. Slab includes a slab.generate function that returns a template string that you can copy and paste to a separate file and include in your own application:

// The original template
var template = '{slab example}Hello {name}{endslab}!';

// Precompile the template
var templates = slab.generate('templateName', template);

The slab CLI Compiler

You can also used the command-line slab utility to generate template files. You can use the compiler using node or the standalone rhino version.


Install Slab via npm:

$ npm install slab

You can then use the slab utility to compile your templates:

$ slab myTemplates.slab > myTemplates.js

Standalone Rhino

Put the slab repository somewhere in your machine, add these lines to your shell's RC files:

# Change /path/to/slab!
export SLABHOME=/path/to/slab

After that you'll be able to run the slab-rhino utility. Simply pass it a set of files and it'll output the generated templates:

$ slab-rhino myTemplates.slab > myTemplates.js


  • Shane Thacker (@shanebo) for the original template syntax idea.
  • DoT JS for the string escaping.

Copyright and License

Copyrighted 2012, Mark Obcena <>, MIT-Style License

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more