Using object properties in spacebars

I have two types of mongo documents that contain an array of strings:

first document:

{
  ...
 spotify: { user: 'bob',
            playlist: 'running'
          }
  ...
}

second document:

{
  ...
 spotify: { user: 'bob',
            track: 'The Black Keys - Fever'
          }
  ...
}

in my template I want to display different html depending on the field present in the document
something like:

{{#if spotify.playlist}}
  <div class="playlist">{{spotify.playlist}}</div>
{{/if}}

{{#if spotify.track}}
  <div class="track">{{spotify.track}}</div>
{{/if}}

in practice there is a lot more html, so just using a helper that returns the class accordingly does not work/would be messy.

Does the approach Iā€™m taking make any sense?
Or do have to work with helpers that check if the fields exist and then return true/false?

You could do something like this:

{{#with spotify.playlist}}
  <div class="playlist">{{this}}</div>
{{/with}}
1 Like

Seems like OK approach to me, but you can use else instead of another #if

And there is always question if you can scope test in helper better to limit unnecessary recomputation runs.

Works! Thanks a lot!