Is it better to use html template (and then html import) to create web components or to use template string? What are pros and cons of these methods?
Asked
Active
Viewed 1,601 times
1 Answers
3
Using html template files is better for reuse: the same file can be used in different web components. Also they are better displayed in most IDEs as they are recognized as full HTML code.
Using template strings is faster (inline). They don't rely on HTML Imports which is not adopted by every browser vendors. Also you can use template literals to insert directly value of JavaScript variables in the DOM.
Actually there's no much diffrences because there's a workaround for every differences list above (i.e. you can reuse template strings if you save them as text file, or you can load html templates withour HTML imports).

Supersharp
- 29,002
- 9
- 92
- 134
-
Thank you. Will it HTML Imports be adopted by all browsers? – asv Jan 13 '17 at 19:27
-
Not Mozilla (until now), the others have not taken position, but the polyfill is fine! – Supersharp Jan 13 '17 at 20:09
-
Is this still a valid answer now that html imports are deprecated? – Grant Noe May 22 '19 at 20:55
-
1@GrantNoe, html template files can be imported via the fetch API – Louise Eggleton Jun 09 '21 at 11:10
-
Yes like here https://stackoverflow.com/a/39872908/4600982 – Supersharp Jun 09 '21 at 16:36