vibe.d beta banner
get vibe.d

Introducing Diet-NG

Header image
Thu, 29 Sep 2016 The Diet template engine is the first part of vibe.d to be elevated to an independent library. The implementation has been rewritten from scratch, with many improvements in all areas.

To give a little background, the Diet template language is aimed at providing a way to define procedurally generated HTML/XML pages (or other output formats), with minimal visual noise. Syntax and feature set are heavily inspired by pug, but instead of JavaScript, all expressions and statements are D statements, and everything that can be done at compile-time is done at compile-time.

A little example, showing some of the basic features:

doctype html
- auto title = "Hello, <World>";
		title #{title} - example page
		h1= title
		h2 Index
			- foreach (i; 0 .. 3)
				li: a(href="##{i}") Point #{i}
		- foreach (i; 0 .. 3)
			h2(id=i) Point #{i}
				These are the #[em contents] of point #{i}. Multiple
				lines of text are contained in this paragraph.

This generates the following output, written to an output range in the most efficient way possible and without any dynamic (heap) memory allocations.

<!DOCTYPE html>
		<title>Hello, &lt;World&gt; - example page</title>
		<h1>Hello, &lt;World&gt;</h1>
		<ol class="pageindex">
			<li><a href="#0">Point 0</a></li>
			<li><a href="#1">Point 1</a></li>
			<li><a href="#2">Point 2</a></li>
		<h2 id="0">Point 0</h2>
		<p>These are the <em>contents</em> of point 0. Multiple
		lines of text are contained in this paragraph.</p>
		<h2 id="1">Point 1</h2>
		<p>These are the <em>contents</em> of point 1. Multiple
		lines of text are contained in this paragraph.</p>
		<h2 id="2">Point 2</h2>
		<p>These are the <em>contents</em> of point 2. Multiple
		lines of text are contained in this paragraph.</p>

Vibe.d currently still contains the original implementation, which was written around the limitations of DMD's CTFE engine back years ago, and is basically a monolithic parser/generator with little flexibility. It also misses a number of convenient language features that are rather hard to implement due to the way the engine is written.

The new implementation has been rewritten cleanly, with separate parser, modifier and generator modules, working on a common DOM tree intermediate representation. The DOM is publicly available and can be arbitrarily modified before passing it to the HTML generator, so that many new applications become possible.

The major new features/improvements are:

  • No external dependencies other than Phobos
  • Extensible/configurable with traits structures
  • Supports inline and nested tags syntax
  • Supports string interpolations within filter nodes (falls back to runtime filters)
  • Supports arbitrary uses other than generating HTML, for example we use it similar to QML/XAML for our internal UI framework
  • The API is @safe and nothrow where possible
  • Uses less memory during compilation
  • Comprehensive unit test suite used throughout development
  • Supports AngularJS special attribute names

The API documentation is not yet available online, but can be shown by running DUB:

dub fetch diet-ng
dub run diet-ng -b ddox

You can try the library directly, or together with vibe.d, starting with the latest alpha releases of 0.7.30. Simply add a dependency to "diet-ng", version "~>1.1". Vibe.d will automatically re-route all render!(...) calls to diet-ng.

GitHub project:

DUB package:

Posted at 15:01:03 +0200 by Sönke Ludwig

Commenting is currently disabled for this article.