htl is an awesome tool inspired by lit-html and HTM.
Look at this code and enjoy :-) MultipleCounters
const {html} = htl
function Counter (start = 0) {
let value = start;
let counter = html`<span>${value}</span>`;
let button = html`<button onclick=${function () {
counter.innerText = ++value;
if (value > 9) {
this.style.display = "none"
counter.innerText = "Limit reached"
}
}}>INC</button>`
function view () {
return html`
<div style="margin-bottom:10px">
${counter}
${button}
</div>`
}
return view()
}
const multipleCounters =
[...Array(50)]
.map(() => Math.floor(Math.random() * 10 ))
.map(Counter)
function App () {
return html`<div>
<h1>Muliple statefull counters</h1>
${multipleCounters}
</div>`
}
document.body.append(App())
Top comments (16)
to be honest, I do not really understand how html
really works. How are the literals handeled here?
This is, what htl exports:
Seems I´m missing something.
This code is also a little confusing to me.
I want neverless show you this code about tagged template litterals
TL
Sorry, but your code seems to be a bit misleading. Did it work for you? Try this:Thank you very much for sharing!
I found this awsome code to create template literals from strings. This is the inversion of your function. This would give us some nice options.
Try this ClickMe
I´m still struggeling to understand the html function, but here ist an example, how it could be. I just did not manage to include _appendBase() into html() directly:
Why not create a wrapper around HTML and return the appended Base
result...
Finally, it´s was that complicated. The code is just a bit confusing. I did not find out, for what reason they used 'Object.assign()' as it works also this way:
We need to append the object after its rendered and postprocessed, so I changed the last line of hypertext(), which is possibly not the smartest solution, but it works.
Now you can write this code, which gives the same result four times:
As ID´s are handled as global variables, you can also access the reference from within Javascript.
I´m not sure, if there are any differences in the way, '${b}' is evaluated, but things seem to be very similar.
Finally, htl boils down to one function:
Any clue, what the advantage of all this hypertextProcessing is?
Couldn't this be a workaround : ClickMe
Calling _html with bracktes renders the template-literal into a string. The result will be the same, but then you do not need _html anymore. See this post
Eckehard
Look at this AppendHTL
Ok, it´s sometimes not easy to see, where a literal starts and ends.
The concept is a bit confusing to me, as htl seems to generate text only?!? So, how do you get DOM references here? If htl would create DOM elements immediately, this would be far more useful i suppose (ok, if you like to work in the DOM directly).
Just because I was curious, how it looks...
html creates actualy DOM elements immediately.
In the following example, title is a DOM element.
HtmlToDOM
What's really nice with this implementation it is that you can embed html elements inside each others
Page
Depends a bit on your task. You will always have some overhead to create your "page". If html could append the objects directly to the DOM (like DML functions do), your code would only have three lines, not 10.
But what is really smart, html creates DOM elements (not text as i thought). So, this code works:
Should not be a big deal to make html append the objects directly...
Hy Eckehard look at this
You can test it here : AppendHTML
Awsome!
Are your sure, this is correct?
Seems something is missing...
Hy Eckehard ,
The Dev To Highlighter is lost :-),
The first parenthesis close the function, the second close '${'
I will use this implementation in the next release of MVU...
Reagards