Skip to content

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

@juliangoacher

Description

@juliangoacher

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions