Template

Struct Template 

Source
pub struct Template;
Expand description

The <template> element - holds HTML content that is not rendered immediately.

§Purpose

The <template> element is used to declare fragments of HTML that can be cloned and inserted into the document via JavaScript. Its content is not rendered when the page loads, making it ideal for client-side templating. The content is parsed but inert until activated.

§Content Categories

  • Metadata Content
  • Flow Content
  • Phrasing Content
  • Script-supporting Element

§Permitted Content Model

  • Anything (content is inert and stored in a DocumentFragment)

§Common Use Cases

  • Client-side HTML templates
  • Repeating UI patterns (list items, cards, etc.)
  • Dynamic content generation
  • Web components and custom elements
  • Avoiding script-based string concatenation

§Key Attributes

  • Global attributes only

§Example

<!-- Template for list items -->
<template id="item-template">
  <li class="item">
    <h3 class="item-title"></h3>
    <p class="item-description"></p>
  </li>
</template>

<ul id="item-list"></ul>

<script>
  const template = document.getElementById('item-template');
  const list = document.getElementById('item-list');
   
  const clone = template.content.cloneNode(true);
  clone.querySelector('.item-title').textContent = 'Title';
  clone.querySelector('.item-description').textContent = 'Description';
  list.appendChild(clone);
</script>

<!-- Card template -->
<template id="card-template">
  <div class="card">
    <img class="card-image" src="" alt="">
    <div class="card-body">
      <h4 class="card-title"></h4>
      <p class="card-text"></p>
      <a class="card-link" href="#">Learn more</a>
    </div>
  </div>
</template>

<!-- Table row template -->
<template id="row-template">
  <tr>
    <td class="col-name"></td>
    <td class="col-email"></td>
    <td class="col-role"></td>
  </tr>
</template>

§WHATWG Specification

Trait Implementations§

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.