linkedin / linkedin/dustjs

Does dust.js support variable substitution in helper param values?

Open
#307 15 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
2.9k
Forks
461
PR merge metrics
No merged PRs in 30d

Description

Given a context like this:

{
  "bar": "fubar",
  "helper": function(chunk, context, bodies, params) {
    return chunk.write(params.foo);
  }
}

and a template like this:

{#helper foo="{bar}"/}

I expected the output: "fubar"
But instead got: "function body_1(chk,ctx){return chk.reference(ctx.get("bar"),ctx,"h");}"

This happens because the dust.js compiler is detecting and compiling the template within the "foo" parameter, but dust isn't then resolving the template before passing the parameter value to the helper function.

It`s possible to resolve the parameter value within the helper function by using chunk.capture:

{
  "bar": "fubar",
  "helper": function(chunk, context, bodies, params) {
    return chunk.capture(params.foo, context, function(value, chunk) {
      chunk.write(value).end();
    });
  }
}

But this approach then runs into problems if more than one parameter value needs to be captured:

{#helper foo="{bar}" foo2="{bar}"/}
  "helper": function(chunk, context, bodies, params) {
    return chunk.capture(params.foo, context, function(value, chunk) {
      chunk.capture(params.foo2, context, function(value2, chunk) {
        chunk.write(value).write(value2).end();
      });
    });

When the above code is used, the two parameter values are output correctly but the template doesn't evaluate beyond the helper function i.e. as if chunk.end() was never called.

The fact that the compiler detects and compiles the template within the parameter value suggests that this functionality is supposed to be supported but isn't fully implemented. Does anyone know if this is the case? Note that given the situation described above, it would be better if the compiler didn't compile param values in this way, as if would then at least give the author of a helper function the option of manually evaluating param values if they were expected to support var substitution.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the helper examples and nested chunk.capture behavior shown in the issue, then trace how dust.js compiles and passes parameter values to helpers. Done should establish whether variable substitution in helper parameters is supported, and either make the behavior consistent or clearly define the alternative semantics.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
web-dev
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.