I have used mustache style templating for a long time on the client and even on the server using mustache PHP. I’m just taking a hard look now at web components and by reading thru articles like this one I’ve got a good idea how the lit-* stuff works and it seems like a strong and lightweight approach.
One thing I still haven’t wrapped my head around is having the templates so integrated with the JavaScript code. I usually kept templates as separate files or as essentially HTML type strings. Do you see any benefits or problems with the templates and code being coupled like this? One of the first things I found myself thinking about lit-HTML was how could I store templates separately for reuse and to be able replace a template used by a component dynamically based on conditions etc.
Any thoughts on this? Perhaps once I put them in use more, the benefits of this approach will be more clear.
Front-end architect at BBVA Next. I've been working for 22 years in Computer Science experimenting with a large number and variety of technologies. I ❤️ web dev specially web components.
Honestly, at first, I was not convinced by the idea of having HTML, CSS and JavaScript in the same file, much less that everything was embedded in a JavaScript class. But I have gotten used to it and now I think it is a matter of personal taste rather than a problem or technical convenience. And I could also say it's a standard considering React and Vue. The good point about it is that I can understand the whole piece just seeing one file.
Anyway, if you want to have the HTML and CSS code in separate files, you can do it, but they will have to be ES6 modules so you can reuse them by importing them.
Imagine you have a template menu and a template footer that you will surely reuse in many pages templates.
So you can have a separate file for each one.
So you can reuse them in your pages by importing each module. (Yes, I know it's not HTML, it has to be a JavaScript module with export clause, but I think it's pretty convenient and thus you get reusable pieces)
home-page.js
import{menu}from'menu.js';import{footer}'footer.js';import{html,render}from'lit-html';constmyData={....}// here you have an object with the information you needconsthomePage=data=>html`
${menu(data)}
<!-- your homePage content here -->
${footer(data)}`;render(homePage(myData),document.body);
I hope I have clarified something. Do you feel OK with this approach?
Yes, that’s great. I wasn’t getting that they could be imported and reused that way. I really like the promise of web components and now they seem to be more like first class parts of JavaScript. Thanks for your response and examples. Time to create some sweet new components 😙
Front-end architect at BBVA Next. I've been working for 22 years in Computer Science experimenting with a large number and variety of technologies. I ❤️ web dev specially web components.
I have used mustache style templating for a long time on the client and even on the server using mustache PHP. I’m just taking a hard look now at web components and by reading thru articles like this one I’ve got a good idea how the lit-* stuff works and it seems like a strong and lightweight approach.
One thing I still haven’t wrapped my head around is having the templates so integrated with the JavaScript code. I usually kept templates as separate files or as essentially HTML type strings. Do you see any benefits or problems with the templates and code being coupled like this? One of the first things I found myself thinking about lit-HTML was how could I store templates separately for reuse and to be able replace a template used by a component dynamically based on conditions etc.
Any thoughts on this? Perhaps once I put them in use more, the benefits of this approach will be more clear.
Hi! Thanks for leaving your comment.
Honestly, at first, I was not convinced by the idea of having HTML, CSS and JavaScript in the same file, much less that everything was embedded in a JavaScript class. But I have gotten used to it and now I think it is a matter of personal taste rather than a problem or technical convenience. And I could also say it's a standard considering React and Vue. The good point about it is that I can understand the whole piece just seeing one file.
Anyway, if you want to have the HTML and CSS code in separate files, you can do it, but they will have to be ES6 modules so you can reuse them by importing them.
Imagine you have a template menu and a template footer that you will surely reuse in many pages templates.
So you can have a separate file for each one.
menu.js
footer.js
So you can reuse them in your pages by importing each module. (Yes, I know it's not HTML, it has to be a JavaScript module with export clause, but I think it's pretty convenient and thus you get reusable pieces)
home-page.js
I hope I have clarified something. Do you feel OK with this approach?
Yes, that’s great. I wasn’t getting that they could be imported and reused that way. I really like the promise of web components and now they seem to be more like first class parts of JavaScript. Thanks for your response and examples. Time to create some sweet new components 😙
Your're welcome! I would like to see those components ;-)