0.2.0
Template literal function for creating HTML templates in Deno.
Repository
Current version released
4 years ago
Versions
html_template
Template literal function for creating HTML templates in Deno. Useful for server side or Worker HTML generation.
Simple Usage
const doc = html`
<!DOCTYPE html>
<html>
<head>
<title>Hello world</title>
</head>
<body>
<p>Hello world</p>
</body>
</html>
`;Inserting Values
You can use normal JS template literal interpolation to insert values. These values will be HTML escaped to prevent XSS in the default usage.
const message = "Hello world";
const doc = html`
<!DOCTYPE html>
<html>
<head>
<title>Hello world</title>
</head>
<body>
<p>${message}</p>
</body>
</html>
`;Inserting HTML into the document
To insert HTML into your document, use TrustedString values for interpolation.
There is an easy helper function, trusted, that will create a TrustedString
and allow for raw HTML interpolation.
const rawHtml = "<h1>My raw HTML</h1>";
const doc = html`
<!DOCTYPE html>
<html>
<head>
<title>Hello world</title>
</head>
<body>
<p>${trusted(rawHtml)}</p>
</body>
</html>
`;Converting HTMLDocument to string
To convert a parsed HTMLDocument back to a string, you can use the
HTML.stringify function. You may also be able to use XMLSerializer if that API
is available.
const doc = html`
<!DOCTYPE html>
<html>
<head>
<title>Hello world</title>
</head>
<body>
<p>Hello world</p>
</body>
</html>
`;
const documentHTML = HTML.stringify(doc);