dhtags 0.1.0

HTML construction library


To use this package, put the following dependency into your project's dependencies section:

dub.json
dub.sdl

D HTML Tags

dhtags is an HTML construction library for D, inspired by ScalaTags.

In short, it takes D code that looks like this:

html(
	head(
    	title("My page"),
  		script(type="text/javascript")("Some script")
    ),
  	body_(
    	div(id="main", cls="greeting")(
      		h1("Hello!"),
      		a(href="//www.example.com")("Example link")
        )
    )
)

and turns it into this:

<html>
	<head>
    	<title>My page</title>
        <script type="text/javascript">Some script</script>
   	</head>
    <body>
    	<div id="main" class="greeting">
        	<h1>Hello!</h1>
            <a href="//www.example.com">Example link</a>
       	</div>
    </body>
</html>
Simple example
import dhtags : div, span, cls;
auto frag = div(span(cls="text")("Some text"));
writeln(frag.toString);
<div><span class="text">Some text</span></div>

Compile-time evaluation is also possible (using enum here).

enum someHtml = div(ul(["Coffee", "Tea", "Milk"].map!(x => li(x)))).toString;
writeln(someHtml);
<div>
	<ul>
		<li>Coffee</li>
    <li>Tea</li>
    <li>Milk</li>
	</ul>
</div>

Note that the HTML here is formatted for easy viewing but the toString function spits it out with no formatting. If you want a pretty-printed string, use toPrettyString.

Attributes

All attributes can take strings but some also can take values of other types like int or bool.

div(contenteditable=true)(
	p(tabindex=1)("First"),
	p(tabindex=2)("Second")
)

You can define custom attributes with .attr like this:

div("my-attr".attr="Hello!")
<div my-attr="Hello!"></div>

Data attributes can be defined in one of two ways. The first method will automatically convert camelcase names to a hyphen-separated name.

div(data.numColumns=10)
div(data("num-columns")=10)
<div data-num-columns="10"></div>
Imports

The preferred way is just import the tags or attributes you need,

import dhtags.tags : div, span;
import dhtags.attrs : id, href;

though this might be impractical in some cases. You can of course import all tags and attributes at once, but beware that this will dump a bunch of stuff into the current namespace. In addition, some tag and attribute names clash so you'll have to prepend these with either tags or attrs respectively.

import dhtags;

auto frag = 
	div(
		tags.form(id="some-form", action="/"),
		button(attrs.form="some-form")("Click me")
	);

writeln(frag.toString);
<div>
	<form id="some-form" action="/"></form>
	<button form="some-form">Click me</button>
</div>
Known Issues
  • Compile-time evaluation of attributes with float or double values is not possible right now. Instead, just use a string, e.g. step="0.1" instead of step=0.1.

Authors:
  • Jatan Patel
Dependencies:
none
Versions:
0.2.1 2017-Nov-04
0.2.0 2016-Apr-15
0.1.0 2016-Apr-05
~master 2017-Nov-08
~develop 2016-Dec-02
Show all 5 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 1 downloads this month

  • 66 downloads total

Score:
0.5
Short URL:
dhtags.dub.pm