# Load a mustache template from file with read node and then inject data in it by a function node

**URL:** <https://discourse.nodered.org/t/load-a-mustache-template-from-file-with-read-node-and-then-inject-data-in-it-by-a-function-node/59702>\
**Category:** General\
**Created:** [11 March 2022 11:09 UTC](https://discourse.nodered.org/t/load-a-mustache-template-from-file-with-read-node-and-then-inject-data-in-it-by-a-function-node/59702 "2022-03-11T11:09:19Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![oliver](https://avatars.discourse-cdn.com/v4/letter/o/85f322/32.png) [@oliver](https://discourse.nodered.org/u/oliver)\
**Post date:** [11 March 2022 11:09 UTC](https://discourse.nodered.org/t/load-a-mustache-template-from-file-with-read-node-and-then-inject-data-in-it-by-a-function-node/59702/1 "2022-03-11T11:09:19Z")

</div>

Hi, is it possible to load a mustache template from file with a read node and then inject data in it by a function node?  
Thanks in advance!

---

<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:** [11 March 2022 11:10 UTC](https://discourse.nodered.org/t/load-a-mustache-template-from-file-with-read-node-and-then-inject-data-in-it-by-a-function-node/59702/2 "2022-03-11T11:10:29Z")

</div>

Yes but it is far easier to use the template node than a function for mustache. Read the built in help for the template node.

---

<div class="post-metadata">

**Author:** ![oliver](https://avatars.discourse-cdn.com/v4/letter/o/85f322/32.png) [@oliver](https://discourse.nodered.org/u/oliver)\
**Post date:** [11 March 2022 11:22 UTC](https://discourse.nodered.org/t/load-a-mustache-template-from-file-with-read-node-and-then-inject-data-in-it-by-a-function-node/59702/3 "2022-03-11T11:22:30Z")

</div>

I tried to do it with a template node already, but that did not work for me... unless you create a flow for every different page that is requested, but that is not what I prefer, I like 1 flow.

I would like to read the requested html page containing Mustache placeholders with the read node, then put it in a template and then inject the data that is pulled out of the SQLite database by JS.

Could not find any example how to do 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:** [11 March 2022 11:42 UTC](https://discourse.nodered.org/t/load-a-mustache-template-from-file-with-read-node-and-then-inject-data-in-it-by-a-function-node/59702/4 "2022-03-11T11:42:03Z")

</div>

Did you read the template node help? You can inject the mustache template in `msg.template`

So load file --\> move file string to msg.template --\> move mustache data to msg.payload --\> template node --\> done

---

<div class="post-metadata">

**Author:** ![oliver](https://avatars.discourse-cdn.com/v4/letter/o/85f322/32.png) [@oliver](https://discourse.nodered.org/u/oliver)\
**Post date:** [11 March 2022 12:21 UTC](https://discourse.nodered.org/t/load-a-mustache-template-from-file-with-read-node-and-then-inject-data-in-it-by-a-function-node/59702/5 "2022-03-11T12:21:10Z")

</div>

Yes of course I read the node help 🙂 and of course I googled a lot , Can you please have a look, because I think it not works as you suggest or I make a mistake. Thanks in advance!

`[{"id":"a0fccc4ff8509992","type":"tab","label":"Flow 3","disabled":false,"info":"","env":[]},{"id":"c7f659cf34a39651","type":"template","z":"a0fccc4ff8509992","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<!DOCTYPE html>\n<html lang=\"en\">\n <head> \n <title>Mustache 1</title>\n </head>\n \n <body>\n \n <h1>Test 2 step injection</h1>\n \n {{payload.myvar}} \n\n </body>\n \n</html>","output":"str","x":740,"y":260,"wires":[["6f6943fd61569095"]]},{"id":"ae6b9adcd967b142","type":"function","z":"a0fccc4ff8509992","name":"FCT1","func":"msg.payload = {\"myvar\" : \"Hillebille\"};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":240,"wires":[["c7f659cf34a39651"]]},{"id":"d870d2023b944585","type":"inject","z":"a0fccc4ff8509992","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":320,"y":240,"wires":[["ae6b9adcd967b142"]]},{"id":"6f6943fd61569095","type":"debug","z":"a0fccc4ff8509992","name":"out template 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":940,"y":260,"wires":[]},{"id":"05df4978256254b6","type":"template","z":"a0fccc4ff8509992","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"","output":"str","x":780,"y":480,"wires":[["b5a4cc2f6441881f"]]},{"id":"58974cbde0f77050","type":"function","z":"a0fccc4ff8509992","name":"FCT2","func":"\nmsg.payload = {\"myvar\" : \"Hillebille\"};\n\nmsg.template = `<!DOCTYPE html>\n<html lang=\"en\">\n <head> \n <title>Mustache 1</title>\n </head>\n \n <body>\n \n <h1>Test 2 step injection</h1>\n \n {{payload.myvar}}\n\n </body>\n \n</html>`;\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":460,"wires":[["05df4978256254b6"]]},{"id":"07fa9339d7882448","type":"inject","z":"a0fccc4ff8509992","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":360,"y":460,"wires":[["58974cbde0f77050"]]},{"id":"b5a4cc2f6441881f","type":"debug","z":"a0fccc4ff8509992","name":"out template 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":980,"y":480,"wires":[]}]`

---

<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:** [11 March 2022 12:22 UTC](https://discourse.nodered.org/t/load-a-mustache-template-from-file-with-read-node-and-then-inject-data-in-it-by-a-function-node/59702/6 "2022-03-11T12:22:31Z")

</div>

Your flow is usable

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![oliver](https://avatars.discourse-cdn.com/v4/letter/o/85f322/32.png) [@oliver](https://discourse.nodered.org/u/oliver)\
**Post date:** [11 March 2022 12:23 UTC](https://discourse.nodered.org/t/load-a-mustache-template-from-file-with-read-node-and-then-inject-data-in-it-by-a-function-node/59702/7 "2022-03-11T12:23:28Z")

</div>

```auto
[{"id":"a0fccc4ff8509992","type":"tab","label":"Flow 3","disabled":false,"info":"","env":[]},{"id":"c7f659cf34a39651","type":"template","z":"a0fccc4ff8509992","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<!DOCTYPE html>\n<html lang=\"en\">\n <head> \n <title>Mustache 1</title>\n </head>\n \n <body>\n \n <h1>Test 2 step injection</h1>\n \n {{payload.myvar}} \n\n </body>\n \n</html>","output":"str","x":740,"y":260,"wires":[["6f6943fd61569095"]]},{"id":"ae6b9adcd967b142","type":"function","z":"a0fccc4ff8509992","name":"FCT1","func":"msg.payload = {\"myvar\" : \"Hillebille\"};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":240,"wires":[["c7f659cf34a39651"]]},{"id":"d870d2023b944585","type":"inject","z":"a0fccc4ff8509992","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":320,"y":240,"wires":[["ae6b9adcd967b142"]]},{"id":"6f6943fd61569095","type":"debug","z":"a0fccc4ff8509992","name":"out template 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":940,"y":260,"wires":[]},{"id":"05df4978256254b6","type":"template","z":"a0fccc4ff8509992","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"","output":"str","x":780,"y":480,"wires":[["b5a4cc2f6441881f"]]},{"id":"58974cbde0f77050","type":"function","z":"a0fccc4ff8509992","name":"FCT2","func":"\nmsg.payload = {\"myvar\" : \"Hillebille\"};\n\nmsg.template = `<!DOCTYPE html>\n<html lang=\"en\">\n <head> \n <title>Mustache 1</title>\n </head>\n \n <body>\n \n <h1>Test 2 step injection</h1>\n \n {{payload.myvar}}\n\n </body>\n \n</html>`;\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":460,"wires":[["05df4978256254b6"]]},{"id":"07fa9339d7882448","type":"inject","z":"a0fccc4ff8509992","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":360,"y":460,"wires":[["58974cbde0f77050"]]},{"id":"b5a4cc2f6441881f","type":"debug","z":"a0fccc4ff8509992","name":"out template 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":980,"y":480,"wires":[]}]

```

---

<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:** [11 March 2022 12:28 UTC](https://discourse.nodered.org/t/load-a-mustache-template-from-file-with-read-node-and-then-inject-data-in-it-by-a-function-node/59702/8 "2022-03-11T12:28:51Z")

</div>

> [@oliver](#):
>
> I think it not works as you suggest or I make a mistake

It works...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/1/a1d0ed52549fc1e6977bc259d117ba831ab9665c.png)

---

<div class="post-metadata">

**Author:** ![oliver](https://avatars.discourse-cdn.com/v4/letter/o/85f322/32.png) [@oliver](https://discourse.nodered.org/u/oliver)\
**Post date:** [11 March 2022 12:31 UTC](https://discourse.nodered.org/t/load-a-mustache-template-from-file-with-read-node-and-then-inject-data-in-it-by-a-function-node/59702/10 "2022-03-11T12:31:42Z")

</div>

Ok great!

Thanks for help! now try with the read node.

---

<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:** [11 March 2022 12:33 UTC](https://discourse.nodered.org/t/load-a-mustache-template-from-file-with-read-node-and-then-inject-data-in-it-by-a-function-node/59702/11 "2022-03-11T12:33:13Z")

</div>

Sorry, but it does work. The template mustache is replaced!

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/30eb1a8e106d54ec855e77dbd2176df9cfb2ec32.png)

---

<div class="post-metadata">

**Author:** ![oliver](https://avatars.discourse-cdn.com/v4/letter/o/85f322/32.png) [@oliver](https://discourse.nodered.org/u/oliver)\
**Post date:** [11 March 2022 13:07 UTC](https://discourse.nodered.org/t/load-a-mustache-template-from-file-with-read-node-and-then-inject-data-in-it-by-a-function-node/59702/12 "2022-03-11T13:07:57Z")

</div>

Ok it works fine with the read node now! Thanks a lot for your help!

```auto
[{"id":"a0fccc4ff8509992","type":"tab","label":"Flow 3","disabled":false,"info":"","env":[]},{"id":"c7f659cf34a39651","type":"template","z":"a0fccc4ff8509992","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<!DOCTYPE html>\n<html lang=\"en\">\n <head> \n <title>Mustache 1</title>\n </head>\n \n <body>\n \n <h1>Test 2 step injection</h1>\n \n {{payload.myvar}} \n\n </body>\n \n</html>","output":"str","x":740,"y":260,"wires":[["6f6943fd61569095"]]},{"id":"ae6b9adcd967b142","type":"function","z":"a0fccc4ff8509992","name":"FCT1","func":"msg.payload = {\"myvar\" : \"Hillebille\"};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":240,"wires":[["c7f659cf34a39651"]]},{"id":"d870d2023b944585","type":"inject","z":"a0fccc4ff8509992","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":320,"y":240,"wires":[["ae6b9adcd967b142"]]},{"id":"6f6943fd61569095","type":"debug","z":"a0fccc4ff8509992","name":"out template 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":940,"y":260,"wires":[]},{"id":"05df4978256254b6","type":"template","z":"a0fccc4ff8509992","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"","output":"str","x":780,"y":480,"wires":[["b5a4cc2f6441881f"]]},{"id":"58974cbde0f77050","type":"function","z":"a0fccc4ff8509992","name":"FCT2","func":"\nmsg.payload = {\"myvar\" : \"Hillebille\"};\n\nmsg.template = `<!DOCTYPE html>\n<html lang=\"en\">\n <head> \n <title>Mustache 1</title>\n </head>\n \n <body>\n \n <h1>Test 2 step injection</h1>\n \n {{payload.myvar}}\n\n </body>\n \n</html>`;\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":460,"wires":[["05df4978256254b6"]]},{"id":"07fa9339d7882448","type":"inject","z":"a0fccc4ff8509992","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":360,"y":460,"wires":[["58974cbde0f77050"]]},{"id":"b5a4cc2f6441881f","type":"debug","z":"a0fccc4ff8509992","name":"out template 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":980,"y":480,"wires":[]},{"id":"b2190841afb11be6","type":"template","z":"a0fccc4ff8509992","name":"","field":"mustache","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"","output":"str","x":1000,"y":640,"wires":[["ecde11e40e9f26f0"]]},{"id":"2b924d73a3d32756","type":"function","z":"a0fccc4ff8509992","name":"FCT2","func":"msg.mustache = {\"myvar\" : \"Hillebille\"};\nmsg.filename = String.raw `D:\\nodered\\mustachebasic.html`;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":640,"wires":[["2afa929cfbbe9d7c"]]},{"id":"0d9c46066de551bd","type":"inject","z":"a0fccc4ff8509992","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":640,"wires":[["2b924d73a3d32756"]]},{"id":"ecde11e40e9f26f0","type":"debug","z":"a0fccc4ff8509992","name":"out template 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1180,"y":640,"wires":[]},{"id":"2afa929cfbbe9d7c","type":"file in","z":"a0fccc4ff8509992","name":"","filename":"","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":620,"y":640,"wires":[["ee4103e2e29e535a","62e54dd95d4c320f"]]},{"id":"ee4103e2e29e535a","type":"debug","z":"a0fccc4ff8509992","name":"output read node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":880,"y":720,"wires":[]},{"id":"62e54dd95d4c320f","type":"function","z":"a0fccc4ff8509992","name":"","func":"msg.template = msg.payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":820,"y":640,"wires":[["b2190841afb11be6"]]}]

```

---

<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:** [10 May 2022 13:08 UTC](https://discourse.nodered.org/t/load-a-mustache-template-from-file-with-read-node-and-then-inject-data-in-it-by-a-function-node/59702/13 "2022-05-10T13:08:45Z")

</div>

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