1

Does Ember have any template helper "get-value-with-key" I found the below usage, but not sure what it does exactly ?

{{get-value-with-key item optionValuePath}}
copenndthagen
  • 49,230
  • 102
  • 290
  • 442
  • 2
    Possible duplicate of [EmberJS - access properties of an object by arbitrary key in a template and have those properties bound using HTMLBars?](http://stackoverflow.com/questions/30676775/emberjs-access-properties-of-an-object-by-arbitrary-key-in-a-template-and-have) – Patsy Issa Feb 26 '16 at 14:58

3 Answers3

2

There is an Ember Get Helper for HTMLBars. You might have to install the Package "ember-get-helper" if you are on ember < 2.1.

{{get object key}}
GerDner
  • 399
  • 1
  • 11
  • and why is my answer wrong? (My answer is correct...) – GerDner Feb 26 '16 at 14:50
  • > You have to install the Package "ember-get-helper". No he does not, `{{get}}` is a helper that is available in all ember apps (depending on version) – Patsy Issa Feb 26 '16 at 14:51
0

Let's say you have the following object:

var obj = {
  "key1": {
    "subkey1": "hello world"
  }
}

Using Ember 3.18, to access "hello world" from the template, you can do:

{{get obj 'key1.subkey1'}}

Big Sam
  • 1,290
  • 14
  • 7
0

You can use the build-in get helper. See docs here: Ember Docs.

Usage example:

{{get object key}}

Note that the get helper will not be able to work on all JavaScript keys. For example, a key with a '.' will not work with the built-in get helper.

For example, if you have a valid JavaScript object like:

const example = {
    'example.pdf': 'pdf_url'
}

// You can access this key normally via
example['example.pdf']

however, this will not work in the get helper

{{get this.example 'example.pdf'}}

One solution is to create a helper that can support the types of keys you need to support. For example, I made a helper that can work on keys with a '.' by including '.' in the key name which are escaped like with ''.

{{get this.example 'example\.pdf'}}

The ember twiddle can be found here: twiddle

Other Helpful Sources:

Arthur Putnam
  • 1,026
  • 1
  • 9
  • 26