Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
HTML templates work perfectly with Web Components. You can define a template once in your HTML, then clone and use it multiple times within your custom elements. When combined with Shadow DOM, you get truly encapsulated, reusable components.
Defines reusable HTML structure
Clone template and populate with data
Encapsulates styles and markup
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Templates in Web Components</title>
</head>
<body>
<div class="container">
<h1>🎨 Templates + Web Components</h1>
<!-- Template stored in HTML -->
<template id="product-card-template">
<div class="product-card">
<div class="product-name"></div>
<div class="product-price"></div>
<div class="product-description"></div>
</div>
</template>
<!-- Use the custom element -->
<div id="products"></div>
<button onclick="addProduct('Laptop', 999.99, 'High-performance laptop with 16GB RAM')">
Add Laptop
</button>
<button onclick="addProduct('Mouse', 29.99, 'Wireless ergonomic mouse')">
Add Mouse
</button>
<button onclick="addProduct('Keyboard', 79.99, 'Mechanical RGB keyboard')">
Add Keyboard
</button>
<div class="info">
<strong>💡 How It Works:</strong>
<p>
The template defines reusable HTML structure. The Web Component clones it,
fills in the data, and uses Shadow DOM for style encapsulation!
</p>
</div>
</div>
</body>
</html>Loading preview...
<template> in HTML with IDcloneNode(true)<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Template Inside Web Component</title>
</head>
<body>
<div class="container">
<h1>🚨 Alert System</h1>
<div class="controls">
<h3 style="color: #10b981; margin-bottom: 16px;">Create New Alert</h3>
<input type="text" id="alert-title" placeholder="Alert Title">
<input type="text" id="alert-message" placeholder="Alert Message">
<select id="alert-type">
<option value="success">Success</option>
<option value="warning">Warning</option>
<option value="error">Error</option>
<option value="info">Info</option>
</select>
<button onclick="createAlert()">Create Alert</button>
</div>
<div id="alerts"></div>
</div>
</body>
</html>Loading preview...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multiple Templates Pattern</title>
</head>
<body>
<div class="container">
<h1>📦 Multi-Template Component System</h1>
<div class="controls">
<button class="btn-user" onclick="addCard('user')">Add User Card</button>
<button class="btn-product" onclick="addCard('product')">Add Product Card</button>
<button class="btn-message" onclick="addCard('message')">Add Message Card</button>
</div>
<div class="grid" id="cards"></div>
</div>
</body>
</html>Loading preview...
cloneNode(true) for deep clones| Pattern | Pros | Cons | Use When |
|---|---|---|---|
| External | Easy to update, Visible in HTML | Requires HTML setup | Simple, static templates |
| Internal | Self-contained, Portable | Harder to modify | Library components |
| Multiple | Flexible, DRY code | More complex | Variants needed |