diff --git a/nodes/core/core/80-template.js b/nodes/core/core/80-template.js index c03388b30..4637da06f 100644 --- a/nodes/core/core/80-template.js +++ b/nodes/core/core/80-template.js @@ -19,11 +19,11 @@ 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); + function NodeContext(msg, nodeContext,parent) { + this.msgContext = new mustache.Context(msg,parent); this.nodeContext = nodeContext; } @@ -31,26 +31,34 @@ module.exports = function(RED) { NodeContext.prototype.lookup = function (name) { // try message first: - var value = this.msgContext.lookup(name); - if (value !== undefined) { - return value; - } - - // try node context: - var dot = name.indexOf("."); - if (dot > 0) { - var contextName = name.substr(0, dot); - var variableName = name.substr(dot + 1); - - if (contextName === "flow" && this.nodeContext.flow) { - return this.nodeContext.flow.get(variableName); + try { + var value = this.msgContext.lookup(name); + if (value !== undefined) { + return value; } - else if (contextName === "global" && this.nodeContext.global) { - return this.nodeContext.global.get(variableName); + + // try node context: + var dot = name.indexOf("."); + if (dot > 0) { + var contextName = name.substr(0, dot); + var variableName = name.substr(dot + 1); + + if (contextName === "flow" && this.nodeContext.flow) { + return this.nodeContext.flow.get(variableName); + } + else if (contextName === "global" && this.nodeContext.global) { + return this.nodeContext.global.get(variableName); + } } + }catch(err) { + throw err; } } + NodeContext.prototype.push = function push (view) { + return new NodeContext(view, this.nodeContext,this.msgContext); + }; + function TemplateNode(n) { RED.nodes.createNode(this,n); this.name = n.name; @@ -64,7 +72,7 @@ module.exports = function(RED) { try { var value; if (node.syntax === "mustache") { - value = mustache.render(node.template, new NodeContext(msg, node.context())); + value = mustache.render(node.template,new NodeContext(msg, node.context())); } else { value = node.template; } diff --git a/test/nodes/core/core/80-template_spec.js b/test/nodes/core/core/80-template_spec.js index 315aef6ae..811e7f179 100644 --- a/test/nodes/core/core/80-template_spec.js +++ b/test/nodes/core/core/80-template_spec.js @@ -166,6 +166,18 @@ describe('template node', function() { }); }); + it('should handle block contexts objects', function(done) { + var flow = [{id:"n1", type:"template", template: "A{{#payload.A}}{{payload.A}}{{.}}{{/payload.A}}B",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('payload','AabcabcB'); + done(); + }); + n1.receive({payload:{A:"abc"}}); + }); + }); it('should raise error if passed bad template', function(done) { var flow = [{id:"n1", type:"template", field: "payload", template: "payload={{payload",wires:[["n2"]]},{id:"n2",type:"helper"}]; helper.load(templateNode, flow, function() {