Zach Cochran
by Zach Cochran
~1 min read

Categories

Tags

Today I ran into a situation where I wanted to actually print the key of a key value pair instead of just printing out the value itself. To do this, we can take advantage of the @key parameter.

Note that this only works when you have an object of size one. If the object is larger than a size of one, only the first item will be used.

Suppose you pass in the following object into your template: {"list":[{"apples": 3}], and you want to be able to dynamically print that key, whatever it may be. You could do the following in your template:

<div>

    {{#each list}}
    <table>
        <tr>
            <td>
                {{@key}}
            </td>
            <td>
                {{this}}
            </td>
        </tr>
    </table>
    {{/each}}

<div>

💚