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="hidden" id="node-input-fieldType">
</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;">
<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">
@ -45,12 +53,13 @@
<div style="height: 250px; min-height:150px;" class="node-text-editor" id="node-input-template-editor" ></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>
<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-output" style="width:180px;">
<option value="str" data-i18n="template.label.plain"></option>
<option value="json" data-i18n="template.label.json"></option>
</select>
</div>
</script>
<script type="text/x-red" data-help-name="template">
@ -83,6 +92,7 @@
format: {value:"handlebars"},
syntax: {value:"mustache"},
template: {value:"This is the payload: {{payload}} !"},
output: {value:"str"}
},
inputs:1,
outputs:1,

View File

@ -19,8 +19,8 @@ module.exports = function(RED) {
var mustache = require("mustache");
/**
* Custom Mustache Context capable to resolve message property and node
* flow and global context
* Custom Mustache Context capable to resolve message property and node
* flow and global context
*/
function NodeContext(msg, nodeContext) {
this.msgContext = new mustache.Context(msg);
@ -58,6 +58,7 @@ module.exports = function(RED) {
this.template = n.template;
this.syntax = n.syntax || "mustache";
this.fieldType = n.fieldType || "msg";
this.outputFormat = n.output || "str";
var node = this;
node.on("input", function(msg) {
@ -68,6 +69,10 @@ module.exports = function(RED) {
} else {
value = node.template;
}
if (node.outputFormat === "json") {
value = JSON.parse(value);
}
if (node.fieldType === 'msg') {
RED.util.setMessageProperty(msg,node.field,value);
} else if (node.fieldType === 'flow') {

View File

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