3
I am currently studying Webcomponents, Polymer, Litelement and so I came across this feature of creating the template using Litelement that I had never seen before:
html`<div>Teste</div>`
Same goes for CSS:
css`div {color: blue}`
The code can be checked here and the result here
Trying to better understand how this statement js went into the source code and found that it is just a function difference:
/**
* Interprets a template literal as an HTML template that can efficiently
* render to and update a container.
*/
export const html = (strings: TemplateStringsArray, ...values: unknown[]) =>
new TemplateResult(strings, values, 'html', defaultTemplateProcessor);
So I tried to implement something like this to understand how it works:
html = (t, i) => {
console.log(t, i);
console.log(typeof t, typeof i);
}
html('teste');
html`teste`;
On the console appears:
What made me understand this statement even less:
<function>`<parameter>`
Where I pass a string and actually I get an object that looks like an array.... after all, what is this statement about? And how do I reach the second variable of the function? What are the real benefits and when should I use this type of statement? Does it have anything to do with String.raw()?