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

Add option to parse Template result as JSON before sending

This commit is contained in:
Nick O'Leary 2017-01-25 17:12:53 +00:00
parent 4affbb8c6b
commit d008b1970c
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
3 changed files with 24 additions and 7 deletions

View File

@ -24,6 +24,14 @@
<input type="text" id="node-input-field" placeholder="payload" style="width:250px;"> <input type="text" id="node-input-field" placeholder="payload" style="width:250px;">
<input type="hidden" id="node-input-fieldType"> <input type="hidden" id="node-input-fieldType">
</div> </div>
<div class="form-row">
<label for="node-input-syntax"><i class="fa fa-code"></i> <span data-i18n="template.label.syntax"></span></label>
<select id="node-input-syntax" style="width:180px;">
<option value="mustache" data-i18n="template.label.mustache"></option>
<option value="plain" data-i18n="template.label.plain"></option>
</select>
</div>
<div class="form-row" style="position: relative; margin-bottom: 0px;"> <div class="form-row" style="position: relative; margin-bottom: 0px;">
<label for="node-input-template"><i class="fa fa-file-code-o"></i> <span data-i18n="template.label.template"></span></label> <label for="node-input-template"><i class="fa fa-file-code-o"></i> <span data-i18n="template.label.template"></span></label>
<input type="hidden" id="node-input-template" autofocus="autofocus"> <input type="hidden" id="node-input-template" autofocus="autofocus">
@ -45,12 +53,13 @@
<div style="height: 250px; min-height:150px;" class="node-text-editor" id="node-input-template-editor" ></div> <div style="height: 250px; min-height:150px;" class="node-text-editor" id="node-input-template-editor" ></div>
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-syntax"><i class="fa fa-code"></i> <span data-i18n="template.label.syntax"></span></label> <label for="node-input-output"><i class="fa fa-long-arrow-right"></i> <span data-i18n="template.label.output"></span></label>
<select id="node-input-syntax" style="width:180px;"> <select id="node-input-output" style="width:180px;">
<option value="mustache" data-i18n="template.label.mustache"></option> <option value="str" data-i18n="template.label.plain"></option>
<option value="plain" data-i18n="template.label.plain"></option> <option value="json" data-i18n="template.label.json"></option>
</select> </select>
</div> </div>
</script> </script>
<script type="text/x-red" data-help-name="template"> <script type="text/x-red" data-help-name="template">
@ -83,6 +92,7 @@
format: {value:"handlebars"}, format: {value:"handlebars"},
syntax: {value:"mustache"}, syntax: {value:"mustache"},
template: {value:"This is the payload: {{payload}} !"}, template: {value:"This is the payload: {{payload}} !"},
output: {value:"str"}
}, },
inputs:1, inputs:1,
outputs:1, outputs:1,

View File

@ -58,6 +58,7 @@ module.exports = function(RED) {
this.template = n.template; this.template = n.template;
this.syntax = n.syntax || "mustache"; this.syntax = n.syntax || "mustache";
this.fieldType = n.fieldType || "msg"; this.fieldType = n.fieldType || "msg";
this.outputFormat = n.output || "str";
var node = this; var node = this;
node.on("input", function(msg) { node.on("input", function(msg) {
@ -68,6 +69,10 @@ module.exports = function(RED) {
} else { } else {
value = node.template; value = node.template;
} }
if (node.outputFormat === "json") {
value = JSON.parse(value);
}
if (node.fieldType === 'msg') { if (node.fieldType === 'msg') {
RED.util.setMessageProperty(msg,node.field,value); RED.util.setMessageProperty(msg,node.field,value);
} else if (node.fieldType === 'flow') { } else if (node.fieldType === 'flow') {

View File

@ -168,8 +168,10 @@
"property": "Set property", "property": "Set property",
"format": "Syntax Highlight", "format": "Syntax Highlight",
"syntax": "Format", "syntax": "Format",
"output": "Output as",
"mustache": "Mustache template", "mustache": "Mustache template",
"plain": "Plain text" "plain": "Plain text",
"json": "Parsed JSON"
}, },
"templatevalue": "This is the payload: {{payload}} !" "templatevalue": "This is the payload: {{payload}} !"
}, },