Lightweight template engine.
JavaScript HTML
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Chat Version Downloads Build Coverage Style

The Ltl template language (pronounced "little") uses a clean Jade-like syntax to generate HTML at doT-like speeds.

If you love tight code and fast rendering, you'll be right at home with Ltl.

Quick Start

Add ltl to your project:

npm install --save ltl

Compile and render templates:

var ltl = require("ltl");
var template = ltl.compile("#hi Hello ${who}!");
var result = template({who: "World"});
<div id="hi">Hello World!</div>


ltl.compile(code, [options])

  • code is a string of Ltl code.
  • options is an object with any of the following properties:
  • name will cause the template to cache at ltl.templates[name]
  • space causes HTML to be indented, using space as indentation.

ltl.setOption(name, value)

  • name is the name of a compiler option.
  • value is the default value you'd like to set it to.

Supported options:

  • tabWidth is the number of spaces that tabs are converted to before compilation. (Default: 4)


Targets are key-value pairs of transpiler names and target language names.



Tag nesting is done with whitespace. You can use tabs or spaces, and Ltl can detect the number of spaces you're using.

    title Hello World!
    div Here is some content.
<!DOCTYPE html>
    <title>Hello World!</title>
    <div>Here is some content.</div>

<!DOCTYPE html> is automagically inserted before an <html> tag. If you would like to specify a custom doctype, you can use the shorthand doctype or ! syntax.

<!DOCTYPE svg>

Nesting can also be done with one-liners using >.

div>span Boo!

IDs and Classes

HTML id and class attributes are done with # and .

div#myId.myClass.myOtherClass Hello
<div id="myId" class="myClass myOtherClass">Hello</div>

When there is no tag name, div is assumed

.hi Hello
<div class="hi">Hello</div>


Attributes are contained in parentheses, and treated like they would be inside an HTML tag.

(style="display:none" data-something="peek-a-boo") Hide me
<div style="display:none;" data-something="peek-a-boo">Hide me</div>

Note: Unlike Jade, Ltl does not use commas between attributes.

Untagged Lines

If you want to insert a line of text without wrapping it in a tag, just start the line with a minus.

  - Hello!
<h1><img src="/logo.png">Hello!</h1>


You can output blocks of content as plain text, using :.

  Bob Loblaw's Law Blog asks, "Why should YOU go
  to jail for a crime someone else noticed?"
<div id="blah">
  Bob Loblaw's Law Blog asks, "Why should YOU go
  to jail for a crime someone else noticed?"

Blocks can also be passed through filters, such as markdown.

  # Ltl
  It's a recursive acronym for "Ltl Template Language".
<h1>ltl</h1><p>It's a recursive acronym for "Ltl Template Language".</p>

If a filter is unrecognized, Ltl will attempt to load it in the following ways:

  • Client-side: use window['FILTER_NAME']
  • Server-side: use require('FILTER_NAME')

A filter must have a function named compile or parse which accepts a state and returns a string, or it can be such a function itself.


Ltl comments are added by using // as a tag, and they do not output any HTML. The // tag can be used on one line or as a block.

h1 Comments
// No one will see this.
p Hello from
  This won't be shown.
  Neither will this.
<h1>Comments</h1><p>Hello from</p>

HTML comments are add by using - as a tag.

- Begin page
p Hello
- End page
  p Delete me
<!--Begin page--><p>Hello</p><!--End page--><!--<p>Delete me</p>-->


You can output the value of a state property with ${..}, and special HTML characters will be escaped for you to prevent silly little XSS attacks.

var code = '. Hello ${name}!';
var template = ltl.compile(code)
template({name: 'Sam'});
<div>Hello Sam!</div>

To encode for a URL rather than HTML, use &{}.

State: {query: "good brewpubs"}

<a href="?q=good%20brewpubs">good brewpubs</a>

If you'd like your content to skip encoding (because you want your expression to output raw HTML tags rather than safely escaped text), use ={..}.

State: {unsafe: "<script>alert('Gotcha!')</script>"}

. ={unsafe}

If you want to show ${..}, &{..} or ={..} blocks in your output, you can escape with a backslash.

code \${escaped} or \={raw}
<code>${escaped} or ={raw}

Variable Assignment

You can assign a value to a variable in the template state using =.

who = 'World'
. Hello ${who}!
<p>Hello World!</p>


Use to iterate over an array inside the state.

State: {list: ['IPA', 'Porter', 'Stout']}

  for item in list
    li ${item}

Use for..of to iterate over object keys.

State: {pairings: {Coffee: 'coding', Beer: 'bloviating'}}

for drink, activity of pairings
  . ${drink} is for ${activity}.
<div><b>Coffee</b> is for <i>coding</i>.</div><div><b>Beer</b> is for <i>bloviating</i>.</div>


Use if, else or else if to render conditionally. The control statement's inline content gets evaluated as JavaScript.

if username == 'root'
  . Do as you please.
else if username
  . Do as you can.
  . Don't.

You can use builtin JavaScript objects and whatnot.

if Math.random() > 0.5
    p This has a 50/50 chance of showing.

Calling templates within templates

A template can call another template with @. To accomplish this, you must compile your templates with, and they will be stored in ltl.cache. The template that's being called can access the data state.

var temp = ltl.compile('p\n @bold', {name: 'temp'});
var bold = ltl.compile('b ${text}', {name: 'bold'});
ltl.cache.temp({text: 'Hi!'});

Templates can receive data using the attribute syntax, and they can receive a nested block of content (if passed), using the builtin scope variable block.

var layout = ltl.compile('html\n head>title ${title}\n body ${block}', {name: 'layout'});
var page = ltl.compile('@layout(title="Test")\n p It worked!', {name: 'page'});;
<!DOCTYPE html>
    <p>It worked!</div>

Template properties

A template can have properties applied to it by using a plus symbol.


  head>title Template Properties
    Properties can be used to provide hidden values to systems that compile
    Ltl templates, such as [Chug](

  When compiled, the template will become a JavaScript function as usual.
  In addition, it will have a property called "extra", whose value will be
  a string containing the contents of this block.

  If the plus symbol is used more than once for the same property, the value
  of that property will be a concatenation of multiple blocks.

  # Also supports filters
  Properties can have filters. This block will be evaluated as markdown,
  and the resulting value will be set as the "also" property of the template.

JS and CSS properties

The js and css properties of a template can be set using the plus symbol, just like other properties. Unlike including JS or CSS in a script or style tag block, these properties would need to be added to a page externally in order to affect the HTML.


p This will be included in the template's rendered HTML.

  console.log("This will not be included in the template's js property.");

  p {color: black}

In addition, several languages that compile to JS/CSS are supported. Their compilers can be invoked using their corresponding file extensions. For JS, Ltl supports coffee, litcoffee, iced, es6, and ts. For CSS, it supports less, scss and styl.


  This template will compile to a function which returns this paragraph, and
  the function will have **js** and **css** properties.

  console.log "Hello from CoffeeScript!"

  @textColor: #000;

  p {
    color: @textColor;

Inline JS and CSS

JavaScript and CSS can also be included inline in a template using directives that appear as tags. Just as with JS and CSS properties, these support compilers such as CoffeeScript and LESS.


  a {color: #000;}

  state.linkText = 'hello'

a ${linkText}

The state variable in a template is called state, so the above would set the linkText value in the state object, and then it would render the following HTML if called:

<style>a {color: #000;}</style><a>hello</a>

More on Ltl...