Search code examples
meteormeteor-blaze

Meteor Blaze Template - Dynamic field name


I'm passing the arguments field1 and field2 to a blaze template and I would like to use them to get an object property inside that template.

For example:

field1 = "name", field2 = "total"

<h5 class="card-item-name">{{this[field1]}}</h5>
<span class="card-item-total">{{this[field2]}}</span>

Instead of:

<h5 class="card-item-name">{{this.name}}</h5>
<span class="card-item-total">{{this.total}}</span>

How can I do this?


Solution

  • Just make yourself a little helper, say:

    thisKey(key) {
     return this[key];
    }
    

    Then in your template use:

    <h5 class="card-item-name">{{thisKey field1}}</h5>
    <span class="card-item-total">{{thisKey field2}}</span>