Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9182081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:28:34+00:00 2026-06-17T18:28:34+00:00

Is it possible to achieve variable indirection in dust.js – and therefore to be

  • 0

Is it possible to achieve variable indirection in dust.js – and therefore to be able to use map-like functionality?

Imagine I have the following context to pass to Dust:

{
  "keys": [ "Foo", "Bar", "Baz" ],
  "data": [{
      "date": "20130101",
      "values": {
        "Foo": 1,
        "Bar": 2,
        "Baz": 3
      }
    }, {
      "date": "20130102",
      "values": {
        "Foo": 4,
        "Bar": 5,
        "Baz": 6
      }
    }]
}

And I want to achieve the following output (it would actually be a table, but I’ve skipped the <tr><td> tags for brevity and replaced them with spaces and newlines):

Date Foo Bar Baz
20130101 1 2 3
20130102 4 5 6

I’m not sure how to loop over the keys property, and use each value x to look up data[i].values[x]. I can get the desired output by hardcoding the keys:

Date{~s}
{#keys}
  {.}{~s}
{/keys}
{~n}
{#data}
  {date}{~s}
  {values.Foo}{~s}
  {values.Bar}{~s}
  {values.Baz}{~s}
  {~n}
{/data}

but the keys will be determined dynamically, so I can’t hardcode them into the template. Is there a way to replace the lines that say values.Foo etc., with something like the following:

{#data}
  {date}{~s}
  {#keys outerMap=values}
    {outerMap.{.}}{~s}
  {/keys}
  {~n}
{/data}

This doesn’t work as written; can I capture the output of {.} (the value of the current key) and dynamically use it as (part of) the property name to resolve?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:28:36+00:00Added an answer on June 17, 2026 at 6:28 pm

    As smfoote says, this is not supported out-of-the-box in Dust.

    However, I’ve realised that handlers can involve some elements of logic, and so it was relatively straightforward to write a handler to do dereferencing:

      deref: function(chunk, context, bodies, params) {
        chunk.write(params.obj[params.prop]);
      }
    

    This handler takes an obj argument which is the object to use as an associative array; and the prop parameter describing the key to look up from that object. With this handler function added to the context, I was then able to write the data part of the template as:

    {#data}
      {date}{~s}
      {#keys}
        {#deref obj=values prop=./}{~s}
      {/keys}
      {~n}
    {/data}
    

    This produces the correct output, by iterating over every key and passing it in as the property to read from the values object.

    I appreciate that some might consider this inappropriate given Dust’s philosophy. However I don’t think it constitutes particularly complex logic; indeed, it’s something that could quite conceivably be part of a templating framework. Given that smfoote’s suggested alternative of updating my JSON isn’t an option (the template does not know what the keys will be in advance, so there is no way to write “static” references here), this seems like a reasonable approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any possible way to achieve this? For instance, I have an I/O
I was just wondering if it was possible to have a variable case using
I don't know if it's possible but i want to achieve this: i have
Is it possible to read the following from the local variable in Lua? local
I have a variable like $path = dir1/dir2/dir1/dir4/; etc.. etc.. I want to remove
is is possible to use macros in config files? I want to achieve something
Is this possible to add variable to callback scope? What I want to achieve
Possible Duplicate: Using variable in SQL LIKE statement I want it to be like
Possible Duplicate: Use of .apply() with 'new' operator. Is this possible? I have 5
Is it possible to have the array subscript before the variable name in declaration?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.