Mustache template to get flow variable with dynamic key

Hi,

I know I can use mustache in a template node to get a flow variable, for example:

{{flow.name}}

However, I can't find out how to do so with a key I am passing with the payload. For example, something like:

{{flow[payload]}}

Is it possible?

I hope it is clear.

Thanks.

Context vars are server side on the node-red device, ui-template is client side on the browser. You would need to move the context vars you require to msg, prior to ui-template node.

I see... ok thanks for your answer.

There are two types of template node here...

Core Node-RED's template node (the orange one) absolutely supports the use of {{ flow.stuff }}.

If you're using the ui-template node in one of the Node-RED Dashboard offerings, then, as @E1cid pointed out, you only have access to the msg object.

The full syntax that the core "template" node supports is Mustache: mustache(5) - Logic-less templates.

and I'm not actually sure it support [] - @Steve-Mcl would be my go-to guru for that.

Accessing values from an object dynamically (e.g. {{flow[payload]}}) is supported in mustache using something called Lamdas

Lambdas

When any value found during the lookup is a callable object, such as a function or lambda, the object will be invoked and passed the block of text. The text passed is the literal block, unrendered. {{tags}} will not have been expanded.

An optional part of the specification states that if the final key in the name is a lambda that returns a string, then that string replaces the content of the section. It will be rendered using the same delimiters (see Set Delimiter below) as the original section content. In this way you can implement filters or caching.

Template:

{{#wrapped}}{{name}} is awesome.{{/wrapped}}

Hash:

{
  "name": "Willy",
  "wrapped": function(text) {
    return "<b>" + text + "</b>"
  }
}

Output:

<b>Willy is awesome.</b>

So, in theory, a mustache template of {{#flow.get}}{{payload}}{{/flow.get}} would work, but it does not.

I can prove the lambda is called but it simply does not work in node-red (likely never explicitly supported this optional feature) e.g:

function

msg.obj = {
    zeroParams: function (param) {
        node.warn('msg.obj.zeroParams was called')
        return 'Hello from msg.obj.zeroParams function'
    },
    oneParam: function (param) {
        node.warn('msg.obj.oneParam was called with param: ' + param)
        return 'Hello from oneParam. I received ' + param
    }
}
return msg;

template

payload: {{payload}}

obj.zeroParams: {{obj.zeroParams}}

#obj.oneParam payload: {{#obj.oneParam}}{{payload}}{{/obj.oneParam}}

As you can see, the lambda is called but it fails to pass the variable and fails to use the return value for lambdas with a parameter. In short, accessing flow.xxx dynamically is not possible.

Whether this is a bug or an unsupported feature is debatable. Feel free to raise an issue on the Node-RED repo.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.