# Using template node and mustache syntax simply

**URL:** <https://discourse.nodered.org/t/using-template-node-and-mustache-syntax-simply/15351>\
**Category:** FAQs\
**Created:** [10 September 2019 08:14 UTC](https://discourse.nodered.org/t/using-template-node-and-mustache-syntax-simply/15351 "2019-09-10T08:14:38Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [10 September 2019 08:14 UTC](https://discourse.nodered.org/t/using-template-node-and-mustache-syntax-simply/15351/1 "2019-09-10T08:14:38Z")

</div>

I've had a lightbulb moment over past 24 hours on how useful the template node can be 🙂

Previously, I'd only ever used it in one flow (and I'd only copied and pasted it so didn't bother working out what it really doing)

I'd only noticed it mentioned when people were talking about stuff with http in/out nodes or dashboard stuff (neither or which I really use) so I just let it woosh over my head.

But I've just realised it it is a VERY simple way of prefixing and appending text to payloads and means I don't have to write JSONata in change nodes or function nodes to get simple jobs done.

So for any others who missed using it simply - here it is

If you have a value (lets say a temperature reading) in msg.payload, then to make that into a nice readable string then simply stick a template node into your flow

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

and amend the text to way you want it

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/6294614f3412cc9ac02fe269cfb02c6bba6a5f95.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/1235d591728c6ebf193fd03d56d7d4c0d93d4dde.png)

I'm now off to amend my flows 🙂

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [10 September 2019 09:39 UTC](https://discourse.nodered.org/t/using-template-node-and-mustache-syntax-simply/15351/2 "2019-09-10T09:39:56Z")

</div>

I concur, it is also very nice to use to create 'new' objects. Sometimes I am bothered by the change node that keeps the payload in order, where one wants to delete/set new properties, it is more work than the template node. I am glad that NR has all the flexibility one wants.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [10 September 2019 09:47 UTC](https://discourse.nodered.org/t/using-template-node-and-mustache-syntax-simply/15351/3 "2019-09-10T09:47:17Z")

</div>

> [@bakman2](#):
>
> it is also very nice to use to create 'new' objects.

Have you got an example of what you mean?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [10 September 2019 09:56 UTC](https://discourse.nodered.org/t/using-template-node-and-mustache-syntax-simply/15351/4 "2019-09-10T09:56:29Z")

</div>

Example payload:

```auto
{"Time":"2019-09-10T11:53:19","Epoch":1568109199,"BME280":{"Temperature":22.39,"Humidity":42.7,"Pressure":1017.3},"BH1750":{"Illuminance":4101},"PressureUnit":"hPa","TempUnit":"C"}

```

if I want to "translate" this to another format, I use the template node (set output to parsed JSON):

```auto
{ 
"temperature": {{ payload.BME280.Temperature }},
"lux": {{payload.BH1750.Illuminance}}
}

```

Output:

```auto
{"temperature":22.6,"lux":4466}

```

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [10 September 2019 22:50 UTC](https://discourse.nodered.org/t/using-template-node-and-mustache-syntax-simply/15351/5 "2019-09-10T22:50:43Z")

</div>

> [@bakman2](#):
>
> if I want to "translate" this to another format, I use the template node (set output to parsed JSON):
> 
> ```auto
> { 
> "temperature": {{ payload.BME280.Temperature }},
> "lux": {{payload.BH1750.Illuminance}}
> }
> 
> ```

That looks strangely familiar to using this JSONata expression in a `change` node:

```auto
payload { 
    "temperature": BME280.Temperature,
    "lux": BH1750.Illuminance
}

```

Just sayin' ... ;\*)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [11 September 2019 06:08 UTC](https://discourse.nodered.org/t/using-template-node-and-mustache-syntax-simply/15351/6 "2019-09-11T06:08:54Z")

</div>

Yes it is a simplistic example 😉

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [11 September 2019 06:18 UTC](https://discourse.nodered.org/t/using-template-node-and-mustache-syntax-simply/15351/7 "2019-09-11T06:18:45Z")

</div>

This is what I've discovered - the template node is really good for simple stuff where all you want to do is re-arrange the msg a bit or add a bit of text to it.

No trying to remember whether JSONata uses + or & to concatenate some text - all you need to know is msg.payload is called {{payload}}

And the default node shows you this as an example so no need to remember if its { {{ or [[ or (( 🙂

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [11 September 2019 18:02 UTC](https://discourse.nodered.org/t/using-template-node-and-mustache-syntax-simply/15351/8 "2019-09-11T18:02:29Z")

</div>

But can the template create an object in msg.payload if the incoming msg.payload is a string?

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [11 September 2019 18:06 UTC](https://discourse.nodered.org/t/using-template-node-and-mustache-syntax-simply/15351/9 "2019-09-11T18:06:10Z")

</div>

Yes  
You just have to make sure the output of the template is valid JSON and then select output as parsed JSON for it work lovely 🙂

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [11 September 2019 18:22 UTC](https://discourse.nodered.org/t/using-template-node-and-mustache-syntax-simply/15351/10 "2019-09-11T18:22:59Z")

</div>

Ok I can't figure it out. Assume I have an inject node with msg payload as a string "Good afternoon" and I want to move it to msg.payload.text. Can you show me an example because I can't figure out how to do it.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [11 September 2019 19:20 UTC](https://discourse.nodered.org/t/using-template-node-and-mustache-syntax-simply/15351/11 "2019-09-11T19:20:04Z")

</div>

The output field for the `template` node would have to be set to be `msg.payload`, and the mustache syntax would be a JSON object with the original payload substituted into a new object, like so:

```auto
{
    "text": "{{ payload }}"
}

```

Be careful to put quotes around the original payload placeholder, unless you can guarantee that the replaced value will not cause some invalid JSON syntax ( white space, punctuation, etc). Also, since the "context" of the musctache evaluator is already set to the input `msg` object, you don't need the "msg." prefix on the payload.

Note that this will cause the existing msg.payload value or object to be _replaced_ by the new object with the one property called "text" -- which may or may not be what you wanted.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [11 September 2019 19:57 UTC](https://discourse.nodered.org/t/using-template-node-and-mustache-syntax-simply/15351/12 "2019-09-11T19:57:30Z")

</div>

Thanks Steve, I thought I had tried that variation, but I tried so many I must have missed that.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [11 September 2019 20:21 UTC](https://discourse.nodered.org/t/using-template-node-and-mustache-syntax-simply/15351/13 "2019-09-11T20:21:22Z")

</div>

But for a simple case like that - it would be easier to actually just use a change node  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/58789217811fd38022c88dfa48fd6b8daf28c960.png)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [11 September 2019 20:48 UTC](https://discourse.nodered.org/t/using-template-node-and-mustache-syntax-simply/15351/14 "2019-09-11T20:48:07Z")

</div>

Very very true and simple (why do I always forget the 'move' option). I should just give up today. I think I am coming down with a cold and my synapes feel like they are moving thru molasses. 🤒

Wait, I got a flu shot Monday, can I blame that?
