1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Change template node to allow templating of any property of msg

This commit is contained in:
Dave C-J 2014-07-10 08:23:44 +01:00
parent 242fe1ef86
commit de352dcdc2
2 changed files with 45 additions and 20 deletions

View File

@ -1,5 +1,5 @@
<!-- <!--
Copyright 2013 IBM Corp. Copyright 2013,2014 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -16,31 +16,33 @@
<script type="text/x-red" data-template-name="template"> <script type="text/x-red" data-template-name="template">
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-template"><i class="icon-wrench"></i> Template</label> <label for="node-input-template"><i class="fa fa-file-code-o"></i> Template</label>
<input type="hidden" id="node-input-template" autofocus="autofocus"> <input type="hidden" id="node-input-template" autofocus="autofocus">
<div style="height: 250px;" class="node-text-editor" id="node-input-template-editor" ></div> <div style="height: 250px;" class="node-text-editor" id="node-input-template-editor" ></div>
</div> </div>
<div class="form-row">
<label for="node-input-field"><i class="fa fa-edit"></i> Property</label>
msg.<input type="text" id="node-input-field" placeholder="payload" style="width: 64%;">
</div>
</script> </script>
<script type="text/x-red" data-help-name="template"> <script type="text/x-red" data-help-name="template">
<p>Creates a new payload based on the provided template.</p> <p>Creates a new message based on the provided template.</p>
<p>This uses the <i><a href="http://mustache.github.io/mustache.5.html" target="_new">mustache</a></i> format.</p> <p>This uses the <i><a href="http://mustache.github.io/mustache.5.html" target="_new">mustache</a></i> format.</p>
<p>For example, when a template of: <p>For example, when a template of:
<pre>Hello {{name}}. Today is {{date}}</pre> <pre>Hello {{name}}. Today is {{date}}</pre>
<p>receives a message containing: <p>receives a message containing:
<pre>{ <pre>{
name: "Fred", name: "Fred",
date: "Monday" date: "Monday"
payload: ... payload: ...
}</pre> }</pre>
<p>The resulting payload will be: <p>The resulting payload will be:
<pre>Hello Fred. Today is Monday</pre> <pre>Hello Fred. Today is Monday</pre>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@ -49,6 +51,7 @@
category: 'function', category: 'function',
defaults: { defaults: {
name: {value:""}, name: {value:""},
field: {value:"payload"},
template: {value:"This is the payload: {{payload}}!"}, template: {value:"This is the payload: {{payload}}!"},
}, },
inputs:1, inputs:1,
@ -86,7 +89,7 @@
url:"templates", // where to get the data from url:"templates", // where to get the data from
type:"template", // the type of object the library is for type:"template", // the type of object the library is for
editor:that.editor, // the field name the main text body goes to editor:that.editor, // the field name the main text body goes to
fields:['name'] fields:['name','field']
}); });
$("#node-input-name").focus(); $("#node-input-name").focus();
}); });
@ -96,5 +99,4 @@
delete this.editor; delete this.editor;
} }
}); });
</script> </script>

View File

@ -17,20 +17,43 @@
module.exports = function(RED) { module.exports = function(RED) {
"use strict"; "use strict";
var mustache = require("mustache"); var mustache = require("mustache");
var util = require("util");
var fs = require('fs'); var fs = require('fs');
function TemplateNode(n) { function TemplateNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.name = n.name; this.name = n.name;
this.field = n.field || "payload";
this.template = n.template; this.template = n.template;
this.on("input", function(msg) { var node = this;
var b = node.field.split(".");
var i = 0;
var m = null;
var rec = function(obj) {
i += 1;
if ((i < b.length) && (typeof obj[b[i-1]] === "object")) {
rec(obj[b[i-1]]); // not there yet - carry on digging
}
else {
if (i === b.length) { // we've finished so assign the value
obj[b[i-1]] = mustache.render(node.template,m);
node.send(m);
}
else {
obj[b[i-1]] = {}; // needs to be a new object so create it
rec(obj[b[i-1]]); // and carry on digging
}
}
}
node.on("input", function(msg) {
if (msg != null) { if (msg != null) {
try { try {
msg.payload = mustache.render(this.template,msg) m = msg;
this.send(msg); i = 0;
rec(msg);
} catch(err) { } catch(err) {
this.error(err.message); node.error(err.message);
} }
} }
}); });