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!
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.
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.
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
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":[]}]
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 ```
)
```
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
[{"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":[]}]
Ok great!
Thanks for help! now try with the read node.
Ok it works fine with the read node now! Thanks a lot for your help!
[{"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"]]}]
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.