# Access context in template node?

**URL:** https://discourse.nodered.org/t/access-context-in-template-node/88801
**Category:** Dashboard
**Tags:** dashboard-2
**Created:** [19 June 2024 15:08 UTC](https://discourse.nodered.org/t/access-context-in-template-node/88801 "2024-06-19T15:08:18Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![dth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dth/32/72723_2.png) [@dth](https://discourse.nodered.org/u/dth)
#### Post date: [19 June 2024 15:08 UTC](https://discourse.nodered.org/t/access-context-in-template-node/88801/1 "2024-06-19T15:08:18Z")

</div>

Is it possible to access node/flow/global context properties from a ui-template node?

As it is now I have to wait for the first message to show anything. It could be useful to either access the data on the server side, and send it to the client along with other initial state. Or to pull the data from the server, during loading/initializing.

(I am setting up a Chart.js graph).

Hoping for some good ideas on that 🙂

---

<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: [19 June 2024 15:20 UTC](https://discourse.nodered.org/t/access-context-in-template-node/88801/2 "2024-06-19T15:20:06Z")

</div>

> Is it possible to access node/flow/global context properties from a ui-template node?

Hi @dth - thanks for the question.

We don't pass the whole `global` or `flow` context store to the client, only `msg`. So you'll need a "change" node ahead of where you want to access, then set _something_ on `msg.` to expose the relevant property/value pairing.

---

<div class="post-metadata">

### Author: ![dth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dth/32/72723_2.png) [@dth](https://discourse.nodered.org/u/dth)
#### Post date: [19 June 2024 15:58 UTC](https://discourse.nodered.org/t/access-context-in-template-node/88801/3 "2024-06-19T15:58:40Z")

</div>

Thanks 🙂

So no ways to load a widget with data from the initial page load (immediately after loading external javascripts, etc. is done)? A change node would have to be messaged after the javascript in the template node is ready.

Could perhaps be an idea to access context values on the server side for embedding data as a javascript array, or whatever, so the widget can have some state, before the first message arrives?

It seems that at least some of the other nodes essentially do this. Like for example a graph showing data, before receiving any messages.

---

<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: [19 June 2024 16:22 UTC](https://discourse.nodered.org/t/access-context-in-template-node/88801/4 "2024-06-19T16:22:52Z")

</div>

We have a `widget-load` event which is applicable here, but isn't exposed via an API to call.

Your best bet, if you want to ensure it's populated each time the widget is loaded is to use a `ui-event`, that will fire on page load, pass data to the `ui-template` node, which can store the `msg`, then once the library is ready, use `this.msg` accordingly.

If the `chart` loads first, the other option you have is to put a `watch` on `msg`, and then act when `payload` is set, checking if the library has loaded or not.

Some docs which may be useful:

- Loading external dependencies: [Template ui-template | Node-RED Dashboard 2.0](https://dashboard.flowfuse.com/nodes/widgets/ui-template.html#loading-external-dependencies)
- Multi-Tenancy Design Pattern: [Getting Started | Node-RED Dashboard 2.0](https://dashboard.flowfuse.com/getting-started.html#multi-tenancy) (whilst you're not using it for multi-tenancy, the `ui-event` pattern described here is still really useful)

---

<div class="post-metadata">

### Author: ![dth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dth/32/72723_2.png) [@dth](https://discourse.nodered.org/u/dth)
#### Post date: [19 June 2024 18:21 UTC](https://discourse.nodered.org/t/access-context-in-template-node/88801/5 "2024-06-19T18:21:20Z")

</div>

Thank you!

---

<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: [19 July 2024 18:22 UTC](https://discourse.nodered.org/t/access-context-in-template-node/88801/6 "2024-07-19T18:22:20Z")

</div>

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