x/markup_tag/mod.ts

Render markup tag easily
Latest
function tag
import { tag } from "https://deno.land/x/markup_tag@0.3.0/mod.ts?s=tag";

Render markup tag.

Parameters

tagName: string[src]

(required)

optional
attributesOrFirstChild: Record<string, string | number | boolean> | string
[src]

(optional)

...children: Array<string>[src]

(optional)

Returns

string[src]

rendered tag

Examples:

import { tag } from "https://deno.land/x/markup_tag/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts"

// common usage
assertEquals(
tag("div", { id: "foo", class: "bar" }, "Hello world!"),
`<div id="foo" class="bar">Hello world!</div>`,
);

// void (no-close) tag
assertEquals(
tag("meta", { charset: "utf-8" }),
`<meta charset="utf-8">`,
);

// nested tags
assertEquals(
tag( "ul", { class: "nav" }, tag("li", "first"), tag("li", "second")),
`<ul class="nav"><li>first</li><li>second</li></ul>`,
);

// boolean attributes
assertEquals(
tag("button", { type: "button", disabled: true }, "disabled"),
`<button type="button" disabled>disabled</button>`,
);

// skip attributes
assertEquals(
tag("input", { type: "text", readonly: false }),
`<input type="text">`,
);