Jekk
Jekk

Reputation: 598

Access object property using variable in dust.js

{#access_map.modifiable_objects}
    <div class='col-md-2 col-xs-4'> 
        <i class="fa {icon}"> </i>
        <span> {name} </span>
        {#mods}
            {>"dust/admin/helpers/form-checkbox" name="{form_name}" label="{display_name}" value="{value}"/}
            <p> {data.powers.{id}} </p>
        {/mods}
    </div>
{/access_map.modifiable_objects}

I have the following code above.

Notice how {data.powers.{id}} is used. My real intention is to do something like : data.powers[<id_name_here>] or data.powers.<id_name_here>;

However, it renders a string. The scope of ID however, SURELY reaches that part where I used it. Any suggestions?

Upvotes: 0

Views: 1193

Answers (2)

Jekk
Jekk

Reputation: 598

Sorry I got the answer.

It's supposed to be {data.powers[id]} and not data.powers[{id}]; It works.

Upvotes: 1

Interrobang
Interrobang

Reputation: 17434

Dust doesn't currently support variable substitution natively. You could write a small helper to do what you want instead.

dust.helpers.get = function(chunk, context, bodies, params) {
  var key = context.get(context.resolve(params.key));
  return chunk.reference(key, context);
};

Then you can use the helper like this:

{@get key="data.powers.{id}" /}

If id were foo, this would output the value of data.powers.foo.

Upvotes: 1

Related Questions