vibe.d beta banner
get vibe.d

Function dietTraits

Marks a struct as a Diet traits container.

A traits struct can contain any of the following:

  • string translate(string) - A function that takes a string and returns the translated version of that string. This is used for translating the text of nodes marked with & at compile time. Note that the input string may contain string interpolations.
  • void filterX(string) - Any number of compile-time filter functions, where "X" is a placeholder for the actual filter name. The first character will be converted to lower case, so that a function filterCss will be available as :css within the Diet template.
  • FilterCallback[string] filters - A dictionary of runtime filter functions that will be used to for filter nodes that don't have an available compile-time filter or contain string interpolations.


diet.traits.DietTraitsAttribute dietTraits() @property;


import diet.html : compileHTMLDietString;
import std.array : appender, array;
import std.string : toUpper;

static struct CTX {
	static string translate(string text) {
		return text == "Hello, World!" ? "Hallo, Welt!" : text;

	static string filterUppercase(I)(I input) {
		return input.toUpper();

auto dst = appender!string;
dst.compileHTMLDietString!("p& Hello, World!", CTX);
assert( == "<p>Hallo, Welt!</p>");

dst = appender!string;
dst.compileHTMLDietString!(":uppercase testing", CTX);
assert( == "TESTING");

/** Translates a line of text based on the traits passed to the Diet parser.

The input text may contain string interpolations of the form `#{...}` or