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-28Lines changed: 26 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,12 @@
1
1
2
-
# Template element
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 استفاده میشود. مرورگر محتوای آن را نادیده میگیرد و تنها اعتبار نحوی آن را بررسی میکند، اما ما میتوانیم با استفاده از JavaScript به آن دسترسی پیدا کنیم و برای ساخت سایر عناصر از آن استفاده کنیم.
5
+
در تئوری، میتوانیم هر عنصر نامرئی دیگری را در HTML برای ذخیرهی کد HTML قرار دهیم. پس چه چیزی `<template>` را خاص میکند؟
6
+
اول از همه، محتوای آن میتواند هر HTML معتبری باشد، حتی اگر معمولاً نیاز به یک تگ محاطکننده داشته باشد.
7
+
برای مثال، میتوانیم یک ردیف جدول `<tr>`را داخل آن قرار دهیم:
5
8
6
-
In theory, we could create any invisible element somewhere in HTML for HTML markup storage purposes. What's special about `<template>`?
7
9
8
-
First, its content can be any valid HTML, even if it normally requires a proper enclosing tag.
9
-
10
-
For example, we can put there a table row `<tr>`:
11
10
```html
12
11
<template>
13
12
<tr>
@@ -16,9 +15,9 @@ For example, we can put there a table row `<tr>`:
16
15
</template>
17
16
```
18
17
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.
18
+
معمولاً اگر سعی کنیم تگ `<tr>`را داخل مثلاً یک `<div>` قرار دهیم، مرورگر ساختار نامعتبر DOM را تشخیص میدهد و آن را "اصلاح" میکند؛ یعنی بهطور خودکار یک `<table>`به اطراف آن اضافه میکند. اما این چیزی نیست که ما بخواهیم. در عوض،`<template>`دقیقاً همان چیزی را که درونش قرار میدهیم، بدون تغییر نگه میدارد.
20
19
21
-
We can put styles and scripts into `<template>`as well:
20
+
ما حتی میتوانیم استایلها و اسکریپتها را نیز داخل `<template>`قرار دهیم:
22
21
23
22
```html
24
23
<template>
@@ -30,18 +29,17 @@ We can put styles and scripts into `<template>` as well:
30
29
</script>
31
30
</template>
32
31
```
32
+
مرورگر محتوای درون `<template>` را «خارج از سند» در نظر میگیرد: استایلها اعمال نمیشوند، اسکریپتها اجرا نمیشوند، تگ `<video autoplay>` پخش نمیشود و غیره.
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.
35
-
36
-
The content becomes live (styles apply, scripts run etc) when we insert it into the document.
34
+
وقتی این محتوا را وارد سند (document) کنیم، فعال میشود (استایلها اعمال میشوند، اسکریپتها اجرا میشوند و غیره).
37
35
38
-
## Inserting template
36
+
## وارد کردن template
39
37
40
-
The template content is available in its `content`property as a [DocumentFragment](info:modifying-document#document-fragment)-- a special type of DOM node.
38
+
محتوای یک template از طریق ویژگی `content`آن در دسترس است که یک [DocumentFragment](info:modifying-document#document-fragment)محسوب میشود — نوع خاصی از گره DOM.
41
39
42
-
We can treat it as any other DOM node, except one special property: when we insert it somewhere, its children are inserted instead.
40
+
میتوانیم با آن مانند هر گرهی دیگری از DOM رفتار کنیم، با یک تفاوت خاص: زمانی که آن را در جایی قرار میدهیم، فقط فرزندانش وارد سند میشوند، نه خود `DocumentFragment`.
43
41
44
-
For example:
42
+
برای مثال:
45
43
46
44
```html run
47
45
<templateid="tmpl">
@@ -64,7 +62,7 @@ For example:
64
62
</script>
65
63
```
66
64
67
-
Let's rewrite a Shadow DOM example from the previous chapter using `<template>`:
65
+
بیایید یک مثال از Shadow DOM را از فصل قبلی با استفاده از `<template>` بازنویسی کنیم:
68
66
69
67
```html run untrusted autorun="no-epub" height=60
70
68
<templateid="tmpl">
@@ -87,9 +85,9 @@ Let's rewrite a Shadow DOM example from the previous chapter using `<template>`:
87
85
</script>
88
86
```
89
87
90
-
In the line `(*)` when we clone and insert `tmpl.content`, as its `DocumentFragment`, its children (`<style>`, `<p>`) are inserted instead.
88
+
در خط `(*)`، زمانی که `tmpl.content` را کپی کرده و درج میکنیم، از آنجایی که یک `DocumentFragment` است، فرزندان آن (یعنی `<style>` و `<p>`) به جای خودش درج میشوند.
91
89
92
-
They form the shadow DOM:
90
+
این عناصر، Shadow DOM را تشکیل میدهند:
93
91
94
92
```html
95
93
<divid="elem">
@@ -99,18 +97,18 @@ They form the shadow DOM:
99
97
</div>
100
98
```
101
99
102
-
## Summary
100
+
## خلاصه
103
101
104
-
To summarize:
102
+
برای جمعبندی:
105
103
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.
104
+
-محتوای `<template>`میتواند هر HTML با ساختار نحوی صحیح باشد.
105
+
-محتوای `<template>`بهعنوان «خارج از سند» در نظر گرفته میشود، بنابراین روی چیزی تأثیر نمیگذارد.
106
+
-ما میتوانیم از طریق JavaScript به `template.content`دسترسی پیدا کنیم و با کپی کردن آن، در یک کامپوننت جدید استفادهاش کنیم.
109
107
110
-
The`<template>`tag is quite unique, because:
108
+
تگ`<template>`ویژگیهای منحصربهفردی دارد، زیرا:
111
109
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.
110
+
-مرورگر ساختار نحوی HTML داخل آن را بررسی میکند (برخلاف استفاده از رشته قالب درون اسکریپت).
111
+
- ...اما همچنان اجازه میدهد که از هر تگ HTML در سطح بالا استفاده شود، حتی تگهایی که بدون تگهای محصورکننده منطقی نیستند (مثل`<tr>`).
112
+
-وقتی محتوا وارد سند شود، تعاملی میشود: اسکریپتها اجرا میشوند، ویدیوهای `<video autoplay>`پخش میشوند و غیره.
115
113
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
+
عنصر`<template>`بهخودیخود هیچ مکانیزمی برای تکرار، اتصال دادهها (data binding) یا جایگزینی متغیرها ندارد، اما میتوانیم این قابلیتها را بر روی آن پیادهسازی کنیم.
0 commit comments