# Mustache template to get flow variable with dynamic key

**URL:** <https://discourse.nodered.org/t/mustache-template-to-get-flow-variable-with-dynamic-key/89797>\
**Category:** Dashboard\
**Tags:** node-red-dashboard, ui-template\
**Created:** [26 July 2024 16:14 UTC](https://discourse.nodered.org/t/mustache-template-to-get-flow-variable-with-dynamic-key/89797 "2024-07-26T16:14:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rawdani](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rawdani/32/29966_2.png) [@rawdani](https://discourse.nodered.org/u/rawdani)\
**Post date:** [26 July 2024 16:14 UTC](https://discourse.nodered.org/t/mustache-template-to-get-flow-variable-with-dynamic-key/89797/1 "2024-07-26T16:14:10Z")

</div>

Hi,

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

```auto
{{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:

```auto
{{flow[payload]}}

```

Is it possible?

I hope it is clear.

Thanks.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 July 2024 16:22 UTC](https://discourse.nodered.org/t/mustache-template-to-get-flow-variable-with-dynamic-key/89797/2 "2024-07-26T16:22:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rawdani](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rawdani/32/29966_2.png) [@rawdani](https://discourse.nodered.org/u/rawdani)\
**Post date:** [26 July 2024 16:23 UTC](https://discourse.nodered.org/t/mustache-template-to-get-flow-variable-with-dynamic-key/89797/3 "2024-07-26T16:23:40Z")

</div>

I see... ok thanks for your answer.

---

<div class="post-metadata">

**Author:** ![joepavitt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/joepavitt/32/59722_2.png) [@joepavitt](https://discourse.nodered.org/u/joepavitt)\
**Post date:** [26 July 2024 17:46 UTC](https://discourse.nodered.org/t/mustache-template-to-get-flow-variable-with-dynamic-key/89797/4 "2024-07-26T17:46:36Z")

</div>

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.](https://mustache.github.io/mustache.5.html)

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 July 2024 08:05 UTC](https://discourse.nodered.org/t/mustache-template-to-get-flow-variable-with-dynamic-key/89797/5 "2024-07-29T08:05:04Z")

</div>

> [@joepavitt](#):
>
> 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:
> 
> ```auto
> {{#wrapped}}{{name}} is awesome.{{/wrapped}}
> 
> ```
> 
> Hash:
> 
> ```auto
> {
> "name": "Willy",
> "wrapped": function(text) {
> return "<b>" + text + "</b>"
> }
> }
> 
> ```
> 
> Output:
> 
> ```auto
> <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

```auto
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

```auto
payload: {{payload}}

obj.zeroParams: {{obj.zeroParams}}

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

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/b/2bd6c1c726d4041ac7ba0f174e9ce0d90f358888.png)

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [12 August 2024 08:05 UTC](https://discourse.nodered.org/t/mustache-template-to-get-flow-variable-with-dynamic-key/89797/6 "2024-08-12T08:05:31Z")

</div>

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