Open Source Dust.js Engine Released

the DocumentAlchemy blog

 
Last Updated:
Open Source Dust.js Engine Released

TL;DR: Please enjoy dust-engine, our own little open-source utility for working with Dust on the command-line, in JavaScript code and within web development frameworks like Express.js. You can find it under DocumentAlchemy/dust-engine on GitHub and it is published as dust-engine on npm.

The DocumentAlchemy team has been working in digital publishing for longer than we care to admit, and in all that that time we've never encountered a web templating framework we love more than Dust.js. The page you are reading now, and most of the web-like content connected with documentalchemy.com, is generated from Dust.js templates.

Dust is a JavaScript-based templating engine first developed by Aleksander Williams (akdubya on GitHub) and now maintained by engineers from LinkedIn, PayPal and elsewhere.

What's so great about Dust? akdubya sums it up well in his description of the design philosophy behind Dust:

Why Dust? Why another templating engine when there are so many alternatives? Dust is based on the philosophy that an ideal templating environment should be:

  • Markup-like: A templating syntax should not encompass operations that are better left to a programming language. Templates should make it easy to format content for presentation while keeping application logic where it belongs: in the application.
  • Asynchronous: Template helpers should be callable asynchronously and in parallel so that expensive operations (caching, deferred loading, filtering) can run as the template is being rendered.
  • Streaming: Templates should allow (but not require) data to be flushed to the output in user-defined chunks.
  • Browser and server compatible: Templates should render in both server and browser environments without hacks or extensive configuration.
  • Storage agnostic: The templating engine should not impose a particular loading scheme. Developers should be free to load templates via the filesystem, a database or an army of carrier pigeons.
  • Composable: Designers should be able to break presentation markup into manageable components and combine these components at runtime. It should not be necessary to statically link templates or manually assemble 'layouts' inside application code.
  • Format agnostic: While HTML generation and DOM manipulation are useful in specific instances, a general-purpose template system should not be tied to a particular output format.
  • Precise: The parser should be accurate enough that designers rarely have to use escape sequences to achieve the desired result. Similarly, templates shouldn't mysteriously generate or eliminate whitespace.
  • Safe(r): At the very least, the engine should be configurable such that it is reasonably safe to render untrusted templates in a server environment.
  • Fast: Server-side templates cannot always be cached. Browser-side templates may be rendered on devices with limited system resources. A template engine should be fast enough to render in real time without bottlenecking its environment.

If this list leaves you nodding your head in agreement you should give Dust a try.

Both before and after forming DA, we've developed our own little idiosyncratic ways of working with Dust.

This week we've finally taken the time to package those conventions for public consumption, and we are proud to announce version 1.0[.2] of dust-engine, our own little open-source utility for working with Dust on the command-line, in JavaScript code and within web development frameworks like Express.js.

(When we say "little", we mean it. The whole thing is roughly 350 non-comment lines of code. But we've found those lines to be quite useful.)

dust-engine can be used to:

  • render Dust.js templates from the command line:
npm install dust-engine
echo "Hello {name}!" > example.dust
dust-engine -t example.dust -j '{"name":"World"}'
  • render Dust.js templates from within JavaScript code:
var render = require('dust-engine').render;
render( "my-template", {my:"context"}, function(err,output) {
  console.log(output);
});
  • and use Dust as an Express.js templating engine:
app.engine('dust', require('dust-engine').renderForExpress);
app.set('view engine', 'dust');
app.get('/hello/:name',function(req,res) {
  res.render( "hello", {name:req.param('name')} );
});

There are quite a few dust-based utilities around, especially for use with Express.js—and many of them are great—but dust-engine happens to (a) cover our use of Dust for both web and non-web templates (Dust is "format agnostic" after all) and (b) be a good fit for our particular style and needs.

We hope you'll check it out at https://github.com/DocumentAlchemy/dust-engine.

About DocumentAlchemy

DocumentAlchemy is a RESTful web service that developers can use to add document generation, conversion and processing features to their app.

Visit our live demonstrations or interactive API reference to explore Document Alchemy's rapidly growing API for working with MS Office documents, PDFs, images, web pages and more.


Copyright © 2017 DocumentAlchemy.