Tag.opIndex

Specify attributes using strings as key-value pairs. Interpolated strings are also accepted.

Note: At present, when using interpolated strings, the equals sign MUST be part of the literal, and cannot be interpolated.

An expression like this is allowed: i"$(key)=$(value)". However, if the equal sign is interpolated, it will produce malformed code. Thus, a piece like i"$("key=value")" would fail.

struct Tag
@safe
opIndex
(
Ts...
)
(
Ts args
)

Examples

Each string (or istring) defines a single attribute. The first equals-sign in the string separates the key from the value:

string output = buildHTML() ~ (html) {
    html.div["id=mydiv", "class=apple orange"] ~ { };
};
assert(output == `<div id="mydiv" class="apple orange"></div>`);

The equals sign is optional. If missing, the value will be empty.

string output = buildHTML() ~ (html) {
    html.input["type=checkbox", "checked"] ~ { };
};
assert(output == `<input type="checkbox" checked=""/>`);

Interpolated strings are supported. Ranges of strings are automatically joined with spaces.

int number = 1;
string[] classes = ["two", "three", "four"];

string output = buildHTML() ~ (html) {
    html.div[i"id=item-$(number)", i"class=one $(classes) five", "checked"] ~ { };
};
assert(output == `<div id="item-1" class="one two three four five" checked=""></div>`);

Attribute names can also be interpolated.

string key = "data-foo";
string output1 = buildHTML() ~ (html) {
    html.div[i"$(key)=value"] ~ { };
};
assert(output1 == `<div data-foo="value"></div>`);

string state = "checked";
string output2 = buildHTML() ~ (html) {
    html.input[i"$(state)", "type=checkbox"] ~ { };
};
assert(output2 == `<input checked="" type="checkbox"/>`);

Interpolated strings are supported. Ranges of strings are automatically joined with spaces.

string key = "data";
string output = buildHTML() ~ (html) {
    html.div[i"$(key)=$(key)=$(key)"] ~ { };
};
assert(output == `<div data="data=data"></div>`);

Meta