Skip to content

Commit 3ba1b6f

Browse files
authored
Merge pull request #384 from RocioC1207/master
Template element
2 parents 3771fa0 + 9b1df5d commit 3ba1b6f

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed
Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,78 @@
11

2-
# Template element
2+
# Elemento template
33

4-
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+
Un elemento `<template>` incorporado sirve como almacenamiento para plantillas de HTML markup. El navegador ignora su contenido, solo verifica la validez de la sintaxis, pero podemos acceder a él y usarlo en JavaScript, para crear otros elementos.
55

6-
In theory, we could create any invisible element somewhere in HTML for HTML markup storage purposes. What's special about `<template>`?
6+
En teoría, podríamos crear cualquier elemento invisible en algún lugar de HTML par fines de almacenamiento de HTML markup. ¿Qué hay de especial en `<template>`?
77

8-
First, its content can be any valid HTML, even if it normally requires a proper enclosing tag.
8+
En primer lugar, su contenido puede ser cualquier HTML válido, incluso si normalmente requiere una etiqueta adjunta adecuada.
99

10-
For example, we can put there a table row `<tr>`:
10+
Por ejemplo, podemos poner una fila de tabla `<tr>`:
1111
```html
1212
<template>
1313
<tr>
14-
<td>Contents</td>
14+
<td>Contenidos</td>
1515
</tr>
1616
</template>
1717
```
1818

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+
Normalmente, si intentamos poner `<tr>` dentro, digamos, de un `<div>`, el navegador detecta la estructura DOM como inválida y la “arregla”, y añade un `<table>` alrededor. Eso no es lo que queremos. Sin embargo, `<template>` mantiene exactamente lo que ponemos allí.
2020

21-
We can put styles and scripts into `<template>` as well:
21+
También podemos poner estilos y scripts dentro de `<template>`:
2222

2323
```html
2424
<template>
2525
<style>
2626
p { font-weight: bold; }
2727
</style>
2828
<script>
29-
alert("Hello");
29+
alert("Hola");
3030
</script>
3131
</template>
3232
```
3333

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+
El navegador considera al contenido `<template>` “fuera del documento”: Los estilos no son aplicados, los scripts no son ejecutados, `<video autoplay>` no es ejecutado, etc.
3535

36-
The content becomes live (styles apply, scripts run etc) when we insert it into the document.
36+
El contenido cobra vida (estilos aplicados, scripts, etc) cuando los insertamos dentro del documento.
3737

38-
## Inserting template
38+
## Insertando template
3939

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+
El contenido template está disponible en su propiedad `content` como un [DocumentFragment](info:modifying-document#document-fragment): un tipo especial de nodo DOM.
4141

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+
Podemos tratarlo como a cualquier otro nodo DOM, excepto por una propiedad especial: cuando lo insertamos en algún lugar, sus hijos son insertados en su lugar.
4343

44-
For example:
44+
Por ejemplo:
4545

4646
```html run
4747
<template id="tmpl">
4848
<script>
49-
alert("Hello");
49+
alert("Hola");
5050
</script>
51-
<div class="message">Hello, world!</div>
51+
<div class="message">¡Hola mundo!</div>
5252
</template>
5353

5454
<script>
5555
let elem = document.createElement('div');
5656
5757
*!*
58-
// Clone the template content to reuse it multiple times
58+
// Clona el contenido de la plantilla para reutilizarlo múltiples veces
5959
elem.append(tmpl.content.cloneNode(true));
6060
*/!*
6161
6262
document.body.append(elem);
63-
// Now the script from <template> runs
63+
// Ahora el script de <template> se ejecuta
6464
</script>
6565
```
6666

67-
Let's rewrite a Shadow DOM example from the previous chapter using `<template>`:
67+
Reescribamos un ejemplo de Shadow DOM del capítulo anterior usando `<template>`:
6868

6969
```html run untrusted autorun="no-epub" height=60
7070
<template id="tmpl">
7171
<style> p { font-weight: bold; } </style>
7272
<p id="message"></p>
7373
</template>
7474

75-
<div id="elem">Click me</div>
75+
<div id="elem">Haz clic sobre mi</div>
7676

7777
<script>
7878
elem.onclick = function() {
@@ -82,14 +82,14 @@ Let's rewrite a Shadow DOM example from the previous chapter using `<template>`:
8282
elem.shadowRoot.append(tmpl.content.cloneNode(true)); // (*)
8383
*/!*
8484
85-
elem.shadowRoot.getElementById('message').innerHTML = "Hello from the shadows!";
85+
elem.shadowRoot.getElementById('message').innerHTML = "¡Saludos desde las sombras!";
8686
};
8787
</script>
8888
```
8989

90-
In the line `(*)` when we clone and insert `tmpl.content`, as its `DocumentFragment`, its children (`<style>`, `<p>`) are inserted instead.
90+
En la línea `(*)`, cuando clonamos e insertamos `tmpl.content` como su `DocumentFragment`, sus hijos (`<style>`, `<p>`) se insertan en su lugar.
9191

92-
They form the shadow DOM:
92+
Ellos forman el shadow DOM:
9393

9494
```html
9595
<div id="elem">
@@ -99,18 +99,18 @@ They form the shadow DOM:
9999
</div>
100100
```
101101

102-
## Summary
102+
## Resumen
103103

104-
To summarize:
104+
Para resumir:
105105

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.
106+
- El contenido `<template>` puede ser cualquier HTML sintácticamente correcto.
107+
- El contenido `<template>` es considerado “fuera del documento”, para que no afecte a nada.
108+
- Podemos acceder a `template.content` desde JavaScript, y clonarlo para reusarlo en un nuevo componente.
109109

110-
The `<template>` tag is quite unique, because:
110+
La etiqueta `<template>` es bastante única, ya que:
111111

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.
112+
- El navegador comprueba la sintaxis HTML dentro de él (lo opuesto a usar una plantilla string dentro de un script).
113+
- ...Pero aún permite el uso de cualquier etiqueta HTML de alto nivel, incluso aquellas que no tienen sentido sin un envoltorio adecuado (por ej.`<tr>`).
114+
- El contenido se vuelve interactivo: cuando es insertado en el documento los scripts se ejecutan, `<video autoplay>` se reproduce, etc.
115115

116-
The `<template>` element does not feature any iteration mechanisms, data binding or variable substitutions, but we can implement those on top of it.
116+
El elemento `<template>` no ofrece ningún mecanismo de iteración, enlazamiento de datos o sustitución de variables, pero podemos implementar los que están por encima.

0 commit comments

Comments
 (0)