Introducing Diet-NG
Introducing Diet-NG
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 https://pugjs.org/, 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>";
html
head
title #{title} - example page
body
h1= title
h2 Index
ol.pageindex
- foreach (i; 0 .. 3)
li: a(href="##{i}") Point #{i}
- foreach (i; 0 .. 3)
h2(id=i) Point #{i}
p.
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>
<html>
<head>
<title>Hello, <World> - example page</title>
</head>
<body>
<h1>Hello, <World></h1>
<h2>Index</h2>
<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>
</ol>
<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>
</body>
</html>
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
andnothrow
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: https://github.com/rejectedsoftware/diet-ng/
DUB package: https://code.dlang.org/packages/diet-ng
Comments for the post are currently disabled.
0 comments