
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
html-template-string
Advanced tools
Dead simple and insanely powerful JavaScript `template-string` to HTML DOM Elements renderer.
Dead simple and insanely powerful JavaScript template-string
to HTML DOM Elements
renderer.
Converts a HTML String in JavaScript to a HTML DOM Element.
When working with WebComponents you’ll find yourself creating a lot of HTML inside JavaScript. Usually you’ll use document.createElement('div')
or the like. Which is a pain in the ass to write but even more painful to read!
So various brilliant people came up with a template string or jsx method to do so. Those other libraries are huge in size and complicated. @tonis2 and I believe that there is a benefit in using our tiny html helper tool. For instance most tools does not allow you to bind events to dom elements inline. Also, often they don’t allow you to create document fragments or the output might not be the same as your input.
After kelbas @tonis2 and I had different believes in how the engine should look like and be used in future so we split up and are following our own path. You’re welcome to do the same, just copy this project and make it work for you or contribute. That is the power of open source.
Have a look at the code, its in fact only one file with ~100 lines of code. It’s nothing new, it’s nothing fancy, it’s just straight up usefull.
Not true. Since this is common practice, you’ll find a lot of plugins that support this scenario.
For Visual Studio Code, I use this one: https://marketplace.visualstudio.com/items?itemName=pushqrdx.inline-html
Add script tagg to your HTML file.
<script type="module" src="https://tjb-webcomponents.github.io/html-template-string/html.min.js"></script>
import html from 'https://tjb-webcomponents.github.io/html-template-string/html.min.js'
npm i -S html-template-string
Then in your code:
import html from 'html-template-string'
To create html elements, as in jsx
, the elements have to have a container element. Otherwise you’ll have to use a document fragment.
const click_event = () => {
window.alert("Click event works!");
}
const element = html`
<div>
<span onclick="${click_event}"><strong>Click me!</strong></span>
<span>Element2</span>
<span>Element3</span>
<div>`
document.body.appendChild(element);
Usually you’ll have to provide an outer container that wraps your element.
But if you wish to create elements without outer container, you can create a document fragment
Document fragments are elements without a real container Document Fragment
const click_event = () => {
window.alert("Click event works!");
}
const list = html`
<data-fragment>
<span onclick="${click_event}"><strong>Click me!</strong></span>
<span>Element2</span>
<span>Element3</span>
<span>Element4</span>
<span>Element5</span>
<span>Element6</span>
</data-fragment>`
document.body.appendChild(list);
Will just render:
<span><strong>Click me!</strong></span>
<span>Element2</span>
<span>Element3</span>
<span>Element4</span>
<span>Element5</span>
<span>Element6</span>
const open_post = () => {
window.alert("Open!");
}
const array = html`<div id="container">
${["post1", "post2", "post3"].map(item => html`<span onclick="${open_post}">${item}</span>`)}
</div>`
document.body.appendChild(array);
const circle = html`<svg height="100" width="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>`;
document.body.appendChild(circle);
FAQs
Dead simple and insanely powerful JavaScript `template-string` to HTML DOM Elements renderer.
The npm package html-template-string receives a total of 11 weekly downloads. As such, html-template-string popularity was classified as not popular.
We found that html-template-string demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.