Function dietTraits
Marks a struct as a Diet traits container.
diet .traits .DietTraitsAttribute dietTraits() @property @safe;
A traits struct can contain any of the following:
string translate(string)
- A function that takes astring
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 functionfilterCss
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 aDocument
to modify its contentsHTMLOutputStyle htmlOutputStyle
- An enum to configure the output style of the generated HTML, e.g. compact or pretty
Example
import diet .html : compileHTMLDietString;
import std .array : appender, array;
import std .string : toUpper;
@dietTraits
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(dst .data == "<p>Hallo, Welt!</p>");
dst = appender!string;
dst .compileHTMLDietString!(":uppercase testing", CTX);
assert(dst .data == "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
`!{...