From f21c8154ed6d44d8f2d0943a96a27f589e105d24 Mon Sep 17 00:00:00 2001 From: Ross Cruickshank Date: Tue, 5 Dec 2017 12:24:06 +0000 Subject: [PATCH] enable template config via msg.template for stored or generated templates (#1503) * updates to 80-template to allow setting template with msg.template * updated 80-template_spec test for msg.template support * fixed 80-template.js test --- nodes/core/core/80-template.html | 3 +++ nodes/core/core/80-template.js | 10 +++++++ test/nodes/core/core/80-template_spec.js | 33 +++++++++++++++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/nodes/core/core/80-template.html b/nodes/core/core/80-template.html index 56c68f3e8..0768a9d17 100644 --- a/nodes/core/core/80-template.html +++ b/nodes/core/core/80-template.html @@ -54,6 +54,9 @@
msg object
A msg object containing information to populate the template.
+
template string
+
A template to be populated from msg.payload. If not configured in the edit panel, + this can be set as a property of msg.

Outputs

diff --git a/nodes/core/core/80-template.js b/nodes/core/core/80-template.js index 2d695825e..14f0350d0 100644 --- a/nodes/core/core/80-template.js +++ b/nodes/core/core/80-template.js @@ -83,6 +83,16 @@ module.exports = function(RED) { node.on("input", function(msg) { try { var value; + /*** + * Allow template contents to be defined externally + * through inbound msg.template IFF node.template empty + */ + if (msg.hasOwnProperty("template")) { + if (node.template == "" || node.template === null) { + node.template = msg.template; + } + } + if (node.syntax === "mustache") { if (node.outputFormat === "json") { value = mustache.render(node.template,new NodeContext(msg, node.context(), null, true)); diff --git a/test/nodes/core/core/80-template_spec.js b/test/nodes/core/core/80-template_spec.js index b59c0f46c..2d2c6b65b 100644 --- a/test/nodes/core/core/80-template_spec.js +++ b/test/nodes/core/core/80-template_spec.js @@ -37,9 +37,40 @@ describe('template node', function() { n2.on("input", function(msg) { msg.should.have.property('topic', 'bar'); msg.should.have.property('payload', 'payload=foo'); + msg.should.have.property('template', '{{payload}}'); done(); }); - n1.receive({payload:"foo",topic: "bar"}); + n1.receive({payload:"foo",topic: "bar", template: "{{payload}}"}); + }); + }); + + it('should modify template from msg.template', function(done) { + var flow = [{id:"n1", type:"template", field:"template", template:"",wires:[["n2"]]},{id:"n2",type:"helper"}]; + helper.load(templateNode, flow, function() { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + msg.should.have.property('topic', 'bar'); + msg.should.have.property('payload', 'foo'); + msg.should.have.property('template', 'payload=foo'); + done(); + }); + n1.receive({payload:"foo", topic: "bar", template: "payload={{payload}}"}); + }); + }); + + it('should modify payload from msg.template', function(done) { + var flow = [{id:"n1", type:"template", field:"payload", template:"",wires:[["n2"]]},{id:"n2",type:"helper"}]; + helper.load(templateNode, flow, function() { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + msg.should.have.property('topic', 'bar'); + msg.should.have.property('payload', 'topic=bar'); + msg.should.have.property('template', 'topic={{topic}}'); + done(); + }); + n1.receive({payload:"foo", topic: "bar", template: "topic={{topic}}"}); }); });