Polymer compatible HTML5 based templating syntax for node.js. Render your templates server-side using the same syntax as in the browser, with no virtual DOM trickery required.
via npm
npm install htmling
Turns this:
<!doctype html>
<html>
<head>
<title>{{title}}</title>
<meta name="description" content="{{description}}">
</head>
<body>
<h1>{{title}}</h1>
<ul>
<template repeat="{{user in users}}">
<li>{{user.name}}</li>
</template>
</ul>
</body>
</html>
plus this:
{
"title": "User List",
"description": "A list of users",
"users": [
{
"name": "Alice"
},
{
"name": "George"
}
]
}
into this:
<!doctype html>
<html>
<head>
<title>User List</title>
<meta name="description" content="A list of users">
</head>
<body>
<h1>User List</h1>
<ul>
<li>Alice</li>
<li>George</li>
</ul>
</body>
</html>
Unlike similar libraries, HTMLing does not require a virtual DOM such as jsDOM. Instead, HTMLing
parses .html
files and transforms them into very efficient executable JavaScript functions.
It uses a parser written in PEG.js which emits a standard Mozilla Parser API AST with some custom node types. The compiler then uses estraverse to convert these custom node types to standard JavaScript expressions. Finally, the result is passed to escodegen which converts the AST into executable JavaScript.
This compilation process happens only once, and the resulting JavaScript is extremely efficient.
HTMLing is easy to integrate with your existing build process, either via the command line or library interfaces.
HTMLing ships with a small command line interface:
The compiled output will be written to STDOUT
htmling ./file.html
The compiled output will be written to compiled.js
.
htmling -o ./compiled.js ./file.html
Compile a nested directory structure to a directory called compiled
. The output
directory will be created if it does not already exist, and the resulting folder structure will
match that of the input.
htmling -o ./compiled ./pages
Compile a nested directory structure to a single called compiled.js
htmling -c -o ./compiled.js ./pages
It's also possible to use HTMLing as a library:
var HTMLing = require('htmling');
var template = HTMLing.string('Hello {{name}}');
console.log(template.render({name: 'Charles'})); // "Hello Charles"
var template = HTMLing.file('./index.html');
console.log(template.render());
var templates = HTMLing.dir('./pages');
console.log(templates.render('index.html', {}))
MIT, see LICENSE.md.