Skip to main content Link Search Menu Expand Document Toggle dark mode Copy Code (external link)
paperwork.help

@key : Item Key

The key (or property name) of the current entry when a `` loop iterates a dictionary or an object. For an array or other collection it is the index of the current item.


On this page

Syntax

{{@key}}
{{#if @key == 'total'}}...{{/if}}

Examples

Data:

builder.WithData("model", new {
    prices = new Dictionary<string, decimal> {
        { "Widget", 9.50m }, { "Gadget", 14.00m }, { "Gizmo", 3.25m }
    }
});

Template:

{{#each model.prices}}
    <p>{{@key}}: {{this}}</p>
{{/each}}

Output:

Widget: 9.50
Gadget: 14.00
Gizmo: 3.25

Nested loops

Each loop has its own key. When an inner loop ends, @key goes back to the key of the outer loop.

{{#each model.regions}}
    <h3>{{@key}}</h3>
    {{#each this}}
        <p>{{@key}} = {{this}}</p>
    {{/each}}
    <p>Finished {{@key}}</p>
{{/each}}

Here the last @key is the region again, not the last key of the inner loop.


Notes

  • Works with Dictionary<,> and other IDictionary objects, Newtonsoft JObject and System.Text.Json objects. Entries are in the order the dictionary or object enumerates them.
  • For a collection, @key is the zero-based index, the same value as @index.
  • @index, @first and @last also work when iterating a dictionary or object.
  • Outside any loop @key is empty.
  • Added in Paperwork.Core 9.8.

See Also