|
1 | 1 | ---
|
2 |
| -title: v-template component |
3 |
| -contributors: [rigor789] |
| 2 | +title: v-template |
| 3 | +contributors: [rigor789, ikoevska] |
4 | 4 | ---
|
5 | 5 |
|
6 |
| -The `v-template` component is used for defining markup that can be reused as a template. |
7 |
| - |
8 |
| -It is utilized internally by the [ListView Component](/en/docs/elements/components/list-view). |
| 6 | +The `<v-template>` component lets you define markup that you can reuse as a template. |
9 | 7 |
|
10 | 8 | ## Props
|
11 | 9 |
|
12 |
| -| name | type | description | |
| 10 | +| Name | Type | Description | |
13 | 11 | |------|------|-------------|
|
14 | 12 | | `if` | `String` | The condition for using this template.
|
15 | 13 | | `name` | `String` | The name of the template, auto-generated if omitted.
|
16 | 14 |
|
17 |
| -# Advanced usage |
18 |
| - |
19 |
| -The rest of this document is meant for advanced users implementing their own components that require a template or multiple templates. |
| 15 | +## Basic usage |
20 | 16 |
|
21 |
| -`v-template` does not render anything when placed into the template, instead it adds a `$templates` property to the parent element or component, which is a [`TemplateBag`](https://github.com/nativescript-vue/nativescript-vue/blob/master/platform/nativescript/runtime/components/v-template.js#L36) instance, and then registers itself as an available template. |
| 17 | +The `<v-template>` component is used internally by the [ListView component](/en/docs/elements/components/list-view) to iterate over its list items. |
22 | 18 |
|
23 |
| -### TemplateBag |
| 19 | +## Advanced usage |
24 | 20 |
|
25 |
| -The `TemplateBag` class is useful for registering multiple templates, and provides a function for selecting the correct template based on the item and the conditions provided for each template. |
| 21 | +You can use `v-template` to implement custom components that require a template or multiple templates. |
26 | 22 |
|
27 |
| -Templates are stored as objects conforming to the [`KeyedTemplate`](https://docs.nativescript.org/api-reference/interfaces/_ui_core_view_.keyedtemplate) interface. |
28 |
| - |
29 |
| -#### The `selectorFn` property |
| 23 | +`v-template` does not render anything when placed in the template. Instead, it adds a `$templates` property to the parent element. This `$templates` property is a [`TemplateBag` instance](https://github.com/nativescript-vue/nativescript-vue/blob/master/platform/nativescript/runtime/components/v-template.js#L36). |
30 | 24 |
|
31 |
| -The `selectorFn` property returns a function that accepts a single parameter which should be an item whose template should be selected. It will go through all the templates registered in the `TemplateBag` and return the first one where the `if` condition returns a truthy value, and if none of the templates match it will return `default`. |
| 25 | +Next, `v-template` registers itself as an available template in the respective `TemplateBag` instance for the parent element. Any existing `TemplateBag` instance on the parent element is reused. |
32 | 26 |
|
33 |
| -#### Available Methods |
| 27 | +### The `TemplateBag` class |
34 | 28 |
|
35 |
| -##### registerTemplate(name: String, condition: String?, scopedFn: Function): void |
| 29 | +The `TemplateBag` class lets you register multiple templates and select the correct template based on the item and the conditions provided for each template. |
36 | 30 |
|
37 |
| - This method is used to register templates into the `TemplateBag` instance, it is mainly used internally. The `scopedFn` should be a render function of a [scoped slot](https://vuejs.org/v2/guide/components.html#Scoped-Slots) |
38 |
| - |
39 |
| -##### getConditionFn(condition: String): Function |
40 |
| - |
41 |
| - This method builds a function that evaluates the given condition. This methods is used internally. |
42 |
| - |
43 |
| -##### getKeyedTemplate(name: String): [KeyedTemplate](https://docs.nativescript.org/api-reference/interfaces/_ui_core_view_.keyedtemplate) |
44 |
| - |
45 |
| -This method returns the `KeyedTemplate` with the given name. |
46 |
| - |
47 |
| -##### patchTemplate(name: String, context: any, oldVnode: VNode?): View |
| 31 | +Templates are stored as objects conforming to the [`KeyedTemplate`](https://docs.nativescript.org/api-reference/interfaces/_ui_core_view_.keyedtemplate) interface. |
48 | 32 |
|
49 |
| -This method will patch an existing `VNode` using the provided `context`. If no `oldVnode` is provided, it will create a new View instance for the given template. |
| 33 | +#### The `selectorFn` property |
50 | 34 |
|
51 |
| -##### getAvailable(): Array<String> |
| 35 | +The `selectorFn` property returns a function that accepts a single parameter. This parameter is the item whose template is selected. |
52 | 36 |
|
53 |
| -Returns an array of available `KeyedTemplates` (returns an array of template names) |
| 37 | +The single-parameter function goes through all templates registered in the `TemplateBag` instance and returns the first one where the `if` condition is met. If none of the templates match, returns `default`. |
54 | 38 |
|
55 |
| -##### getKeyedTemplates(): Array<KeyedTemplate> |
| 39 | +#### Available methods |
56 | 40 |
|
57 |
| -Returns an array of all the `KeyedTemplates` registered in the `TemplateBag` |
| 41 | +| Method | Description | |
| 42 | +|---|---| |
| 43 | +| `registerTemplate(name: String, condition: String?, scopedFn: Function): void` | _Mainly used internally._<br/>Registers templates in the `TemplateBag` instance.<br/>The `scopedFn` should be a render function of a [scoped slot](https://vuejs.org/v2/guide/components.html#Scoped-Slots) | |
| 44 | +| `getConditionFn(condition: String): Function` | _Used internally._<br/>Builds a function that evaluates the given condition. | |
| 45 | +| `getAvailable(): Array<String>` | Returns an array of available [`KeyedTemplates`](https://docs.nativescript.org/api-reference/interfaces/_ui_core_view_.keyedtemplate). (Returns an array of template names.) | |
| 46 | +| `getKeyedTemplate(name: String): KeyedTemplate` | Returns the [`KeyedTemplate`](https://docs.nativescript.org/api-reference/interfaces/_ui_core_view_.keyedtemplate) with the specified name. | |
| 47 | +| `getKeyedTemplates(): Array<KeyedTemplate>` | Returns an array of all the [`KeyedTemplates`](https://docs.nativescript.org/api-reference/interfaces/_ui_core_view_.keyedtemplate) registered in the `TemplateBag`. | |
| 48 | +| `patchTemplate(name: String, context: any, oldVnode: VNode?): View` | Patches an existing [`VNode`](https://vuejs.org/v2/guide/render-function.html#The-Virtual-DOM) using the provided `context`. If no `oldVnode` is provided, creates a new View instance for the specified template. | |
0 commit comments