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.
  • SafeFilterCallback[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.
  • alias processors = AliasSeq!(...) - A list of callables taking a Document to modify its contents
  • HTMLOutputStyle htmlOutputStyle - An enum to configure the output style of the generated HTML, e.g. compact or pretty
@property
DietTraitsAttribute
dietTraits
(
)

Examples

1 import diet.html : compileHTMLDietString;
2 import std.array : appender, array;
3 import std.string : toUpper;
4 
5 @dietTraits
6 static struct CTX {
7 	static string translate(string text) {
8 		return text == "Hello, World!" ? "Hallo, Welt!" : text;
9 	}
10 
11 	static string filterUppercase(I)(I input) {
12 		return input.toUpper();
13 	}
14 }
15 
16 auto dst = appender!string;
17 dst.compileHTMLDietString!("p& Hello, World!", CTX);
18 assert(dst.data == "<p>Hallo, Welt!</p>");
19 
20 dst = appender!string;
21 dst.compileHTMLDietString!(":uppercase testing", CTX);
22 assert(dst.data == "TESTING");

Meta