# template

Version: 0.5.0
Framework commit: d503df2befa756e681fcdc6250e9552dc7388bea
Canonical URL: /docs/0.5.0/api/ferroui-view/prelude/template/function-template/

**function** · [`ferroui-view`](/docs/0.5.0/api/ferroui-view/) · v0.5.0

```rust
fn template<T, E>(view: impl Fn(T) -> E + 'static) -> std::rc::Rc<dyn IDataTemplate>
where T: Clone + 'static,
      E: IntoElement
```

A data template whose item is a component: for the `ItemTemplate` of an
items control or the `ContentTemplate` of a content control.

```ignore
list_box()
    .items_source(people)
    .item_template(template(|person: Person| rsx! { TextBlock { text: person.name } }))
```

Each item container gets a [`ComponentHost`](/docs/0.5.0/api/ferroui-view/prelude/struct-componenthost/) whose component runs `view`
with the item (the `DataContext` of the container). The template matches
data of type `T`. What `view` makes sees the contexts of the owner that was
current when the template was made.

The template recycles. A host that leaves the visual tree (a virtualizing
panel took its container away, a list removed its item) keeps its
component mounted, with its last item, and waits in a pool of the
template. The next item that needs a host takes one from the pool: its
component runs again with the new item and the reconciler patches its
controls, so scrolling a long list makes no new controls once the pool has
the hosts of a screen. The pool ends its hosts when the template is
dropped.

[View source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.View/template.rs#L35)

## Availability

Extracted for `aarch64-apple-darwin`. Cargo features are listed in the [crate index](/docs/0.5.0/api/).
