A shorthand for elemX. Available if importing elemi.xml.
Add a new node as a child.
Create an XML element.
Add a new node as a child.
Elemi allows creating XML elements using the elemX template.
auto element = elemX!"paragraph"("Hello, World!"); assert(element == "<paragraph>Hello, World!</paragraph>");
Elements can be nested.
auto element = elemX!"book"( elemX!"title"("Programming in D"), elemX!"author"("Ali Çehreli"), elemX!"link"("https://www.ddili.org/ders/d.en/index.html"), ); assert(element == `<book>` ~ `<title>Programming in D</title>` ~ `<author>Ali Çehreli</author>` ~ `<link>https://www.ddili.org/ders/d.en/index.html</link>` ~ `</book>`);
Text and XML content can be mixed together. Special XML characters are always escaped.
auto element = elemX!"paragraph"( "<injection />", elemX!"bold"("Can't get through me!"), ); assert(element == `<paragraph>` ~ `<injection />` ~ `<bold>Can't get through me!</bold>` ~ `</paragraph>`);
Attributes can be added using attr at the beginning of your element.
auto book = elemX!"website"( attr("title") = "D Programming Language", attr("url") = "https://dlang.org", "D is a general-purpose programming language with static typing, ", "systems-level access, and C-like syntax. With the D Programming ", "Language, write fast, read fast, and run fast." ); assert(book == `<website ` ~ `title="D Programming Language" ` ~ `url="https://dlang.org">` ~ `D is a general-purpose programming language with static typing, ` ~ `systems-level access, and C-like syntax. With the D Programming ` ~ `Language, write fast, read fast, and run fast.` ~ `</website>`);
You can create self-closing tags if you follow the name with a slash.
auto element = elemX!"void/"(); assert(element == "<void/>");
The elem shorthand defaults to HTML mode, but if you import elemi.xml directly, XML mode will be used.
{ import elemi : elem; auto e = elem!"input"(); assert(e == "<input/>"); // <input> is a self-closing void tag } { import elemi.xml : elem; auto e = elem!"input"(); assert(e == "<input></input>"); }
If you need to add children dynamically, you can append them.
Element element = elemX!"parent"(); // Append elements foreach (content; ["one", "two", "three"]) { element ~= elemX!"child"(content); } // Append attributes element ~= attr("id") = "example"; assert(element == `<parent id="example">` ~ `<child>one</child>` ~ `<child>two</child>` ~ `<child>three</child>` ~ `</parent>`);
If you need to group a few elements together, you can do it with elems.
auto child = elems( "Hello, ", elemX!"strong"("World"), ); auto parent = elemX!"div"( child, ); assert(child == "Hello, <strong>World</strong>"); assert(parent == "<div>Hello, <strong>World</strong></div>");
If combined with a fresh compiler, Elemi also supports interpolated strings.
auto e = elemX!"item"( attr("expression") = i"1+2 = $(1+2)", i"1+2 is $(1+2)", ); assert(e == `<item expression="1+2 = 3">1+2 is 3</item>`);
elemX also supports DTD declarations like DOCTYPE, XML declarations, and even preprocessor tags.
assert(elemX!"!DOCTYPE"("html") == `<!DOCTYPE html>`); assert(elemX!"?xml"( attr("version") = "1.0", attr("encoding") = "UTF-8") == `<?xml version="1.0" encoding="UTF-8" ?>`); assert(elemX!"?php"(`$var = "<div></div>";`) == `<?php $var = "<div></div>"; ?>`); assert(elemX!"?="(`$var`) == `<?= $var ?>`);
This module defines the old Elemi syntax for generating XML code.
See examples for more information.