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
Copy file name to clipboardExpand all lines: 8-web-components/4-template-element/article.md
+26-26Lines changed: 26 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,14 @@
1
1
2
2
# Template element
3
3
4
-
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 (HTML Markup Templates) عمل میکند. مرورگر محتوای درون آن را نادیده میگیرد و فقط از نظر صحت نگارشی (Syntax) آن را بررسی میکند، اما ما میتوانیم با استفاده از 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
+
10
+
برای مثال، ما میتوانیم یک ردیف جدول <tr> را درون آن قرار دهیم:
9
11
10
-
For example, we can put there a table row `<tr>`:
11
12
```html
12
13
<template>
13
14
<tr>
@@ -16,9 +17,9 @@ For example, we can put there a table row `<tr>`:
16
17
</template>
17
18
```
18
19
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.
20
+
معمولاً اگر سعی کنیم تگ `<tr>`را داخل مثلاً یک `<div>` قرار دهیم، مرورگر ساختار نامعتبر DOM را تشخیص میدهد و آن را "اصلاح" میکند؛ یعنی بهطور خودکار یک `<table>`به اطراف آن اضافه میکند. اما این چیزی نیست که ما بخواهیم. در عوض،`<template>`دقیقاً همان چیزی را که درونش قرار میدهیم، بدون تغییر نگه میدارد.
20
21
21
-
We can put styles and scripts into `<template>`as well:
22
+
ما حتی میتوانیم استایلها و اسکریپتها را نیز داخل `<template>`قرار دهیم:
22
23
23
24
```html
24
25
<template>
@@ -30,18 +31,17 @@ We can put styles and scripts into `<template>` as well:
30
31
</script>
31
32
</template>
32
33
```
34
+
مرورگر محتوای درون `<template>` را «خارج از سند» در نظر میگیرد: استایلها اعمال نمیشوند، اسکریپتها اجرا نمیشوند، تگ `<video autoplay>` پخش نمیشود و غیره.
33
35
34
-
The browser considers `<template>` content "out of the document": styles are not applied, scripts are not executed, `<video autoplay>` is not run, etc.
35
-
36
-
The content becomes live (styles apply, scripts run etc) when we insert it into the document.
36
+
وقتی این محتوا را وارد سند (document) کنیم، فعال میشود (استایلها اعمال میشوند، اسکریپتها اجرا میشوند و غیره).
37
37
38
-
## Inserting template
38
+
## وارد کردن 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
+
محتوای یک 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 رفتار کنیم، با یک تفاوت خاص: زمانی که آن را در جایی قرار میدهیم، فقط فرزندانش وارد سند میشوند، نه خود `DocumentFragment`.
43
43
44
-
For example:
44
+
برای مثال:
45
45
46
46
```html run
47
47
<templateid="tmpl">
@@ -64,7 +64,7 @@ For example:
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">
@@ -87,9 +87,9 @@ Let's rewrite a Shadow DOM example from the previous chapter using `<template>`:
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
+
در خط `(*)`، زمانی که `tmpl.content` را کپی کرده و درج میکنیم، از آنجایی که یک `DocumentFragment` است، فرزندان آن (یعنی `<style>` و `<p>`) به جای خودش درج میشوند.
91
91
92
-
They form the shadow DOM:
92
+
این عناصر، Shadow DOM را تشکیل میدهند:
93
93
94
94
```html
95
95
<divid="elem">
@@ -99,18 +99,18 @@ They form the shadow DOM:
99
99
</div>
100
100
```
101
101
102
-
## Summary
102
+
## خلاصه
103
103
104
-
To summarize:
104
+
برای جمعبندی:
105
105
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
+
-محتوای `<template>`میتواند هر HTML با ساختار نحوی صحیح باشد.
107
+
-محتوای `<template>`بهعنوان «خارج از سند» در نظر گرفته میشود، بنابراین روی چیزی تأثیر نمیگذارد.
108
+
-ما میتوانیم از طریق JavaScript به `template.content`دسترسی پیدا کنیم و با کپی کردن آن، در یک کامپوننت جدید استفادهاش کنیم.
109
109
110
-
The`<template>`tag is quite unique, because:
110
+
تگ`<template>`ویژگیهای منحصربهفردی دارد، زیرا:
111
111
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
+
-مرورگر ساختار نحوی HTML داخل آن را بررسی میکند (برخلاف استفاده از رشته قالب درون اسکریپت).
113
+
- ...اما همچنان اجازه میدهد که از هر تگ HTML در سطح بالا استفاده شود، حتی تگهایی که بدون تگهای محصورکننده منطقی نیستند (مثل`<tr>`).
114
+
-وقتی محتوا وارد سند شود، تعاملی میشود: اسکریپتها اجرا میشوند، ویدیوهای `<video autoplay>`پخش میشوند و غیره.
115
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.
116
+
عنصر`<template>`بهخودیخود هیچ مکانیزمی برای تکرار، اتصال دادهها (data binding) یا جایگزینی متغیرها ندارد، اما میتوانیم این قابلیتها را بر روی آن پیادهسازی کنیم.
0 commit comments