Skip to content

Commit fb8b32f

Browse files
authored
Cross-link sample app and enhance examples (#20003)
1 parent 1ace32d commit fb8b32f

1 file changed

Lines changed: 45 additions & 2 deletions

File tree

aspnetcore/blazor/components/templated-components.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ Templated components are components that accept one or more UI templates as para
1818
* A table component that allows a user to specify templates for the table's header, rows, and footer.
1919
* A list component that allows a user to specify a template for rendering items in a list.
2020

21+
[View or download sample code](https://github.com/dotnet/AspNetCore.Docs/tree/master/aspnetcore/blazor/common/samples/) ([how to download](xref:index#how-to-download-a-sample))
22+
2123
## Template parameters
2224

2325
A templated component is defined by specifying one or more component parameters of type <xref:Microsoft.AspNetCore.Components.RenderFragment> or <xref:Microsoft.AspNetCore.Components.RenderFragment%601>. A render fragment represents a segment of UI to render. <xref:Microsoft.AspNetCore.Components.RenderFragment%601> takes a type parameter that can be specified when the render fragment is invoked.
2426

25-
`TableTemplate` component:
27+
`TableTemplate` component (`TableTemplate.razor`):
2628

2729
[!code-razor[](../common/samples/3.x/BlazorWebAssemblySample/Components/TableTemplate.razor)]
2830

@@ -39,6 +41,21 @@ When using a templated component, the template parameters can be specified using
3941
<td>@context.Name</td>
4042
</RowTemplate>
4143
</TableTemplate>
44+
45+
@code {
46+
private List<Pet> pets = new List<Pet>
47+
{
48+
new Pet { PetId = 2, Name = "Mr. Bigglesworth" },
49+
new Pet { PetId = 4, Name = "Salem Saberhagen" },
50+
new Pet { PetId = 7, Name = "K-9" }
51+
};
52+
53+
private class Pet
54+
{
55+
public int PetId { get; set; }
56+
public string Name { get; set; }
57+
}
58+
}
4259
```
4360

4461
> [!NOTE]
@@ -59,6 +76,10 @@ Component arguments of type <xref:Microsoft.AspNetCore.Components.RenderFragment
5976
<td>@pet.Name</td>
6077
</RowTemplate>
6178
</TableTemplate>
79+
80+
@code {
81+
...
82+
}
6283
```
6384

6485
Alternatively, you can specify the `Context` attribute on the component element. The specified `Context` attribute applies to all specified template parameters. This can be useful when you want to specify the content parameter name for implicit child content (without any wrapping child element). In the following example, the `Context` attribute appears on the `TableTemplate` element and applies to all template parameters:
@@ -74,11 +95,15 @@ Alternatively, you can specify the `Context` attribute on the component element.
7495
<td>@pet.Name</td>
7596
</RowTemplate>
7697
</TableTemplate>
98+
99+
@code {
100+
...
101+
}
77102
```
78103

79104
## Generic-typed components
80105

81-
Templated components are often generically typed. For example, a generic `ListViewTemplate` component can be used to render `IEnumerable<T>` values. To define a generic component, use the [`@typeparam`](xref:mvc/views/razor#typeparam) directive to specify type parameters:
106+
Templated components are often generically typed. For example, a generic `ListViewTemplate` component (`ListViewTemplate.razor`) can be used to render `IEnumerable<T>` values. To define a generic component, use the [`@typeparam`](xref:mvc/views/razor#typeparam) directive to specify type parameters:
82107

83108
[!code-razor[](../common/samples/3.x/BlazorWebAssemblySample/Components/ListViewTemplate.razor)]
84109

@@ -90,6 +115,20 @@ When using generic-typed components, the type parameter is inferred if possible:
90115
<li>@pet.Name</li>
91116
</ItemTemplate>
92117
</ListViewTemplate>
118+
119+
@code {
120+
private List<Pet> pets = new List<Pet>
121+
{
122+
new Pet { Name = "Mr. Bigglesworth" },
123+
new Pet { Name = "Salem Saberhagen" },
124+
new Pet { Name = "K-9" }
125+
};
126+
127+
private class Pet
128+
{
129+
public string Name { get; set; }
130+
}
131+
}
93132
```
94133

95134
Otherwise, the type parameter must be explicitly specified using an attribute that matches the name of the type parameter. In the following example, `TItem="Pet"` specifies the type:
@@ -100,4 +139,8 @@ Otherwise, the type parameter must be explicitly specified using an attribute th
100139
<li>@pet.Name</li>
101140
</ItemTemplate>
102141
</ListViewTemplate>
142+
143+
@code {
144+
...
145+
}
103146
```

0 commit comments

Comments
 (0)