Skip to content

Commit f152ed3

Browse files
authored
Merge pull request #368 from samanzameni/master
Template element
2 parents 54a4aeb + aefec5b commit f152ed3

File tree

6 files changed

+26
-28
lines changed

6 files changed

+26
-28
lines changed

8-web-components/4-template-element/article.md

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11

2-
# Template element
2+
# Template element (عنصر قالب)
33

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>`را داخل آن قرار دهیم:
58

6-
In theory, we could create any invisible element somewhere in HTML for HTML markup storage purposes. What's special about `<template>`?
79

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>`:
1110
```html
1211
<template>
1312
<tr>
@@ -16,9 +15,9 @@ For example, we can put there a table row `<tr>`:
1615
</template>
1716
```
1817

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>` دقیقاً همان چیزی را که درونش قرار می‌دهیم، بدون تغییر نگه می‌دارد.
2019

21-
We can put styles and scripts into `<template>` as well:
20+
ما حتی می‌توانیم استایل‌ها و اسکریپت‌ها را نیز داخل `<template>` قرار دهیم:
2221

2322
```html
2423
<template>
@@ -30,18 +29,17 @@ We can put styles and scripts into `<template>` as well:
3029
</script>
3130
</template>
3231
```
32+
مرورگر محتوای درون `<template>` را «خارج از سند» در نظر می‌گیرد: استایل‌ها اعمال نمی‌شوند، اسکریپت‌ها اجرا نمی‌شوند، تگ `<video autoplay>` پخش نمی‌شود و غیره.
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.
35-
36-
The content becomes live (styles apply, scripts run etc) when we insert it into the document.
34+
وقتی این محتوا را وارد سند (document) کنیم، فعال می‌شود (استایل‌ها اعمال می‌شوند، اسکریپت‌ها اجرا می‌شوند و غیره).
3735

38-
## Inserting template
36+
## وارد کردن template
3937

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.
4139

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`.
4341

44-
For example:
42+
برای مثال:
4543

4644
```html run
4745
<template id="tmpl">
@@ -64,7 +62,7 @@ For example:
6462
</script>
6563
```
6664

67-
Let's rewrite a Shadow DOM example from the previous chapter using `<template>`:
65+
بیایید یک مثال از Shadow DOM را از فصل قبلی با استفاده از `<template>` بازنویسی کنیم:
6866

6967
```html run untrusted autorun="no-epub" height=60
7068
<template id="tmpl">
@@ -87,9 +85,9 @@ Let's rewrite a Shadow DOM example from the previous chapter using `<template>`:
8785
</script>
8886
```
8987

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>`) به جای خودش درج می‌شوند.
9189

92-
They form the shadow DOM:
90+
این عناصر، Shadow DOM را تشکیل می‌دهند:
9391

9492
```html
9593
<div id="elem">
@@ -99,18 +97,18 @@ They form the shadow DOM:
9997
</div>
10098
```
10199

102-
## Summary
100+
## خلاصه
103101

104-
To summarize:
102+
برای جمع‌بندی:
105103

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` دسترسی پیدا کنیم و با کپی کردن آن، در یک کامپوننت جدید استفاده‌اش کنیم.
109107

110-
The `<template>` tag is quite unique, because:
108+
تگ `<template>` ویژگی‌های منحصربه‌فردی دارد، زیرا:
111109

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>` پخش می‌شوند و غیره.
115113

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) یا جایگزینی متغیرها ندارد، اما می‌توانیم این قابلیت‌ها را بر روی آن پیاده‌سازی کنیم.
492 Bytes
Loading
492 Bytes
Loading
492 Bytes
Loading
492 Bytes
Loading
438 Bytes
Loading

0 commit comments

Comments
 (0)