elemi.wrapper

Wrappers allow defining custom HTML/XML structures that take children as content.

This module builds upon elemi.generator.

See examples for usage instructions!

Members

Aliases

mustuse
alias mustuse = AliasSeq!()
Undocumented in source.

Functions

buildWrapper
WrapperBuilder buildWrapper()

Prepare a wrapper. Connect with a one parameter function: buildWrapper ~ (content) { }. The argument given, content, is a function, which can be called to write user-provided content.

Structs

SystemWrapper
struct SystemWrapper

A wrapper element that can only be used in @system code.

Wrapper
struct Wrapper

A wrapper element that can be used in @safe code.

WrapperBuilder
struct WrapperBuilder

A wrapper builder. Create using buildWrapper.

Examples

Wrappers make it possible to place children inside chunks of premade HTML or XML code. A wrapper is given a function — content in this example — which writes user-given code to the output stream.

import elemi;

auto text = buildHTML() ~ (html) {

    // Wrapper: <span></span><div>CONTENT</div>
    // Prepends an empty span node, and wraps the content in a div.
    auto divWrapper = buildWrapper() ~ (content) {
        html.span ~ { },
        html.div ~ {
            content();
        };
    };

    // Place text content inside the wrapper
    divWrapper ~ {
        html ~ "First wrapper";
    };
    divWrapper ~ {
        html ~ "Second wrapper";
    };
};
assert(text == "<span></span><div>First wrapper</div><span></span><div>Second wrapper</div>");

Wrappers are especially useful when created in a function.

import elemi;

Wrapper section(HTML html, string title) {
    return buildWrapper() ~ (content) {
        html.div ~ {
            html.h1 ~ title;
            content();
        };
    };
}

auto text = buildHTML() ~ (html) {
    section(html, "Title") ~ {
        html.p ~ "Content";
    };
};

assert(text == "<div><h1>Title</h1><p>Content</p></div>");

Meta

History

  • Introduced in Elemi 1.4.0