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 it contents, only checks for syntax validity, but we can access and use it in JavaScript, to create other elements.
4
+
L'elemento built-in `<template>`funziona come uno storage per i templates del markup HTML. Il browser ignora il suo contenuto, controllandone solamente la validità della sintassi, ma possiamo accedervi ed usarli via JavaScript, per creare altri elementi.
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
+
In teoria, possiamo creare qualunque elemento invisibile in qualunque punto dell'HTML, al solo scopo di salvare del markup HTML. Cos'altro possiamo dire su`<template>`?
7
7
8
-
First, its content can be any valid HTML, even if it normally requires a proper enclosing tag.
8
+
Prima di tutto il contenuto può essere qualunque HTML valido, anche se, normalmente richiede un tag di inclusione appropriato.
9
9
10
-
For example, we can put there a table row`<tr>`:
10
+
Per esempio, possiamo inserire la riga di una tabella`<tr>`:
11
11
```html
12
12
<template>
13
13
<tr>
14
-
<td>Contents</td>
14
+
<td>Contenuti</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
+
Solitamente, se proviamo a inserire un`<tr>`, per esempio, dentro un `<div>`, il browser riconoscerà una struttura non valida, "correggendola", aggiungendovi una tabella attorno. Questo non è il comportamento corretto, ma d'altra parte`<template>`mantiene le cose esattamente come le inseriamo.
20
20
21
-
We can put styles and scripts into `<template>`as well:
21
+
Dentro i tag `<template>`possiamo anche inserire stili e scripts:
22
22
23
23
```html
24
24
<template>
@@ -31,17 +31,17 @@ We can put styles and scripts into `<template>` as well:
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
+
Il browser considera il contenuto di `<template>`"avulso dal documento": gli stili non verranno applicati, gli script non verranno eseguiti, i `<video autoplay>`non partiranno in automatico, etc.
35
35
36
-
The content becomes live (styles apply, scripts run etc) when we insert it into the document.
36
+
Il contenuto prende vita (gli stili vengono applicati, gli script vengono eseguiti, etc) solo quando viene inserito dentro il documento.
37
37
38
-
## Inserting template
38
+
## Inserimento del template
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
+
Il contenuto del template è disponibile nella proprietà `content`come [DocumentFragment](info:modifying-document#document-fragment), un particolare tipo di nodo 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
+
Possiamo trattarlo come ogni altro nodo del DOM, tranne per una sua peculiarità: quando lo inseriamo da qualche parte, come DocumentFragment, vengono inseriti solo i suoi figli.
43
43
44
-
For example:
44
+
Per esempio:
45
45
46
46
```html run
47
47
<templateid="tmpl">
@@ -55,24 +55,24 @@ For example:
55
55
let elem =document.createElement('div');
56
56
57
57
*!*
58
-
//Clone the template content to reuse it multiple times
58
+
//Clona il contenuto del template per riutilizzarlo piu' volte
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
+
//Ora lo script dentro <template> viene eseguito
64
64
</script>
65
65
```
66
66
67
-
Let's rewrite a Shadow DOM example from the previous chapter using`<template>`:
67
+
Riscriviamo un esempio di Shadow DOM, preso dal capitolo precedente, usando però`<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">Cliccami</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>`:
elem.shadowRoot.getElementById('message').innerHTML="Hello from the shadows!";
85
+
elem.shadowRoot.getElementById('message').innerHTML="Saluti dalle ombre!";
86
86
};
87
87
</script>
88
88
```
89
89
90
-
In the line `(*)`when we clone and insert `tmpl.content`, as its `DocumentFragment`, its children (`<style>`, `<p>`) are inserted instead.
90
+
Nella riga `(*)`quando cloniamo ed inseriamo `tmpl.content`, come `DocumentFragment`, vengono inseriti i suoi figli (`<style>`, `<p>`).
91
91
92
-
They form the shadow DOM:
92
+
Questi formano lo shadow DOM:
93
93
94
94
```html
95
95
<divid="elem">
@@ -99,18 +99,16 @@ They form the shadow DOM:
99
99
</div>
100
100
```
101
101
102
-
## Summary
102
+
## Riepilogo
103
103
104
-
To summarize:
104
+
- il contenuto di `<template>` può essere qualunque HTML sintatticamente corretto.
105
+
- il contenuto di `<template>` essendo considerato "avulso dal documento", non modificherà, né eseguirà alcunché al suo interno.
106
+
- Possiamo accedere a `template.content` via JavaScript, ed anche clonarlo per poterlo riutilizzare in un nuovo componente.
105
107
106
-
-`<template>` content can be any syntactically correct HTML.
107
-
-`<template>` content is considered "out of the document", so it doesn't affect anything.
108
-
- We can access `template.content` from JavaScript, clone it to reuse in a new component.
108
+
Il tag `<template>` ha delle peculiarità uniche, in quanto:
109
109
110
-
The `<template>` tag is quite unique, because:
110
+
- Il browser controlla la sintassi HTML al suo interno (diversamente dall'uso di un template string dentro uno script).
111
+
- ...Tuttavia è permesso l'uso di tag HTML top-level, anche quelli che non avrebbero alcun senso se privi del loro appropriato contenitore (per esempio i `<tr>`).
112
+
- Il contenuto diventa interattivo (gli scripts vengono eseguiti, i `<video autoplay>` partono, etc) quando viene inserito dentro il documento.
111
113
112
-
- The browser checks HTML syntax inside it (as opposed to using a template string inside a script).
113
-
- ...But still allows use of any top-level HTML tags, even those that don't make sense without proper wrappers (e.g. `<tr>`).
114
-
- The content becomes interactive: scripts run, `<video autoplay>` plays etc, when inserted into the document.
115
-
116
-
The `<template>` element does not feature any iteration mechanisms, data binding or variable substitutions, but we can implement those on top of it.
114
+
L'elemento `<template>` non comprende alcun tipo di meccanismo di interazione, data binding o sostituzioni di variabili, ma possiamo implementarli.
0 commit comments