How to use dynamic keys in dust to map content in properties file

1.7k views Asked by At

I am getting a JSON from contollers and on the basisi of value of this JSON i want to set key in my dust file, so that this key will access data from properties file.

For example I am getting "step" from controllers.

   {
      step: step1
   }

I want to set key in dust:

   {@pre type="content" key="{step}"/}

and in properties I have

step1=This is dynamic key
step2=print this

but i am getting step1 in dust. its not accesing the values of keys. Can anyone suggest me how to display values associated with keys in this situtaion.

4

There are 4 answers

0
myusuf On
{@pre type="content" key=step/}

when step is a variable. To set a string, ex: 'stackoverflow', try:

{@pre type="content" key="stackoverflow"/}
0
Interrobang On

Is this your own pre helper?

You need to change the behavior of the helper to adjust what it does with the param.

function(chunk, context, bodies, params) {
  // Assuming I have {@pre key="{step}" /}
  params.key; // <== this will be a Dust function that, when run, returns "step1"
  dust.helpers.tap(params.key, chunk, context); // <== the string "step1"
  context.get(dust.helpers.tap(params.key, chunk, context)); // <== the string "This is dynamic key"
}

You want to evaluate the parameter, then get the corresponding key from the context, so you want the third form.

dust.helpers.tap requires the dustjs-helpers addon library.

0
rragan On

In kraken, the @pre helper is not a real dust helper.

https://github.com/krakenjs/makara#how-do-i-reference-content-in-a-dust-template

It has the syntax of one but it is a placeholder that is replaced at build time by the content string with the matching key value from the .properties file. The build time behavior means that you cannot dynamically select the content element to use at runtime.

Your best bet is to select the desired value dynamically in your controller and pass the resulting content string as a data element in the model. Then you can reference it in your template.

See here for an example of referencing a bundle element by key: https://github.com/krakenjs/makara#example

0
Venkat.R On

I tried like below and it worked.

JSON

{
  step: step1,
  data: {
    pageName: "Hello"
  }
}

Code

{@pre type="content" key=step /}
{@pre type="content" key=data.pageName /}