You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A built-in`<template>`element serves as a storage for HTML markup templates. The browser ignores its contents, only checks for syntax validity, but we can access and use it in JavaScript, to create other elements.
4
+
Вбудований елемент`<template>`використовується як вмістилище для шаблонної розмітки HTML. Браузер не бере до уваги його вміст, лише перевіряє на правильність синтаксису. Ми можемо доступитися та використовувати його у JavaScript, щоб створювати інші елементи.
5
5
6
-
In theory, we could create any invisible element somewhere in HTML for HTML markup storage purposes. What's special about`<template>`?
6
+
Теоретично, ми могли б створити будь-який невидимий елемент де-небудь у HTML для зберігання у ньому розмітки HTML. Тоді що ж такого особливого у`<template>`?
7
7
8
-
First, its content can be any valid HTML, even if it normally requires a proper enclosing tag.
8
+
По-перше, він може містити у собі будь-який коректний HTML, навіть якщо за звичайних умов він би потребував якогось додаткового тега.
9
9
10
-
For example, we can put there a table row`<tr>`:
10
+
Наприклад, ми можемо помістити тег рядка таблиці`<tr>`:
11
11
```html
12
12
<template>
13
13
<tr>
14
-
<td>Contents</td>
14
+
<td>Вміст</td>
15
15
</tr>
16
16
</template>
17
17
```
18
18
19
-
Usually, if we try to put`<tr>`inside, say, a`<div>`, the browser detects the invalid DOM structure and "fixes" it, adds `<table>` around. That's not what we want. On the other hand, `<template>`keeps exactly what we place there.
19
+
Зазвичай, якщо ми пробуємо помістити тег`<tr>`всередину, скажімо, тега`<div>`, браузер помічає некоректну структуру DOM та "виправляє" її, додаючи навколо `<table>`. Але це не те, що ми хочемо. На противагу цьому, `<template>`зберігає дані у тому вигляді, у якому ми їх туди помістили.
20
20
21
-
We can put styles and scripts into `<template>`as well:
21
+
Ми також можемо помістити всередину `<template>`стилі чи скрипт:
22
22
23
23
```html
24
24
<template>
25
25
<style>
26
26
p { font-weight: bold; }
27
27
</style>
28
28
<script>
29
-
alert("Hello");
29
+
alert("Привіт");
30
30
</script>
31
31
</template>
32
32
```
33
33
34
-
The browser considers `<template>`content "out of the document": styles are not applied, scripts are not executed, `<video autoplay>`is not run, etc.
34
+
Браузер розглядає вміст тега `<template>`як такий, який існує "за межами документа": до нього не застосовуються стилі, не виконуються скрипти, `<video autoplay>`не запускається і тому подібне.
35
35
36
-
The content becomes live (styles apply, scripts run etc) when we insert it into the document.
36
+
Вміст починає опрацьовуватися (застосовуються стилі, виконуються скрипти і т. ін.), коли ми вставляємо його у документ.
37
37
38
-
## Inserting template
38
+
## Вставка шаблону
39
39
40
-
The template content is available in its `content`property as a [DocumentFragment](info:modifying-document#document-fragment) -- a special type of DOM node.
40
+
Вміст template доступний у його властивості `content`як [DocumentFragment](info:modifying-document#document-fragment) -- спеціальний тип вузла DOM.
41
41
42
-
We can treat it as any other DOM node, except one special property: when we insert it somewhere, its children are inserted instead.
42
+
Ми можемо працювати з ним як зі всіма іншими вузлами DOM, за винятком однієї особливості: коли ми його кудись вставляємо, вставляються його дочірні елементи, а не він сам.
43
43
44
-
For example:
44
+
Наприклад:
45
45
46
46
```html run
47
47
<templateid="tmpl">
48
48
<script>
49
-
alert("Hello");
49
+
alert("Привіт");
50
50
</script>
51
-
<divclass="message">Hello, world!</div>
51
+
<divclass="message">Привіт, світ!</div>
52
52
</template>
53
53
54
54
<script>
55
55
let elem =document.createElement('div');
56
56
57
57
*!*
58
-
//Clone the template content to reuse it multiple times
58
+
//Клонувати вміст шаблону, для його подальшого використання
59
59
elem.append(tmpl.content.cloneNode(true));
60
60
*/!*
61
61
62
62
document.body.append(elem);
63
-
//Now the script from <template> runs
63
+
//Тепер запускається скрипт з <template>
64
64
</script>
65
65
```
66
66
67
-
Let's rewrite a Shadow DOM example from the previous chapter using`<template>`:
67
+
Перепишемо приклад Shadow DOM з попереднього розділу, використовуючи`<template>`:
68
68
69
69
```html run untrusted autorun="no-epub" height=60
70
70
<templateid="tmpl">
71
71
<style>p { font-weight: bold; } </style>
72
72
<pid="message"></p>
73
73
</template>
74
74
75
-
<divid="elem">Click me</div>
75
+
<divid="elem">Клікни мене</div>
76
76
77
77
<script>
78
78
elem.onclick=function() {
@@ -82,14 +82,14 @@ Let's rewrite a Shadow DOM example from the previous chapter using `<template>`:
0 commit comments