Skip to content

Commit 28d1cdd

Browse files
ikoevskarigor789
authored andcommitted
rework-v-template (#154)
* rework-v-template Reworked for style and format * Fixed table headings * Addressed feedback
1 parent 666af29 commit 28d1cdd

File tree

1 file changed

+25
-34
lines changed

1 file changed

+25
-34
lines changed
+25-34
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,48 @@
11
---
2-
title: v-template component
3-
contributors: [rigor789]
2+
title: v-template
3+
contributors: [rigor789, ikoevska]
44
---
55

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

108
## Props
119

12-
| name | type | description |
10+
| Name | Type | Description |
1311
|------|------|-------------|
1412
| `if` | `String` | The condition for using this template.
1513
| `name` | `String` | The name of the template, auto-generated if omitted.
1614

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
2016

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

23-
### TemplateBag
19+
## Advanced usage
2420

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

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).
3024

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

33-
#### Available Methods
27+
### The `TemplateBag` class
3428

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

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

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
5034

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

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

55-
##### getKeyedTemplates(): Array<KeyedTemplate>
39+
#### Available methods
5640

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

Comments
 (0)