UNPKG

node-red-contrib-render

Version:

Node for node-red to render a moustache template passed into the node

84 lines (79 loc) 3.32 kB
<script type="text/x-red" data-template-name="render"> <div class="form-row"> <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label> <input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name"> </div> <div class="form-row"> <label for="node-input-field"><i class="fa fa-edit"></i> <span data-i18n="template.label.property"></span></label> <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-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="render"> <p>Sets a property based on the template provided in <code>msg.template</code>.</p> <h3>Inputs</h3> <dl class="message-properties"> <dt>msg <span class="property-type">object</span></dt> <dd>A msg object containing information to populate the template.</dd> <dt>msg.template <span class="property-type">string</span></dt> <dd>A string containing a valid template to render.</dd> </dl> <h3>Outputs</h3> <dl class="message-properties"> <dt>msg <span class="property-type">object</span></dt> <dd>a msg with a property set by populating the configured template with properties from the incoming msg.</dd> </dl> <h3>Details</h3> <p>This uses the <i><a href="http://mustache.github.io/mustache.5.html" target="_blank">mustache</a></i> format.</p> <p>For example, when a template of: <pre>Hello {{payload.name}}. Today is {{date}}</pre> <p>receives a message containing: <pre>{ date: "Monday" payload: { name: "Fred", } }</pre> <p>The resulting property will be: <pre>Hello Fred. Today is Monday</pre> <p>It is possible to use a property from the flow context or global context. Just use <code>{{flow.name}}</code> or <code>{{global.name}}</code>. <p><b>Note: </b>By default, <i>mustache</i> will escape any HTML entities in the values it substitutes. To prevent this, use <code>{{{triple}}}</code> braces. </script> <script type="text/javascript"> RED.nodes.registerType('render',{ color:"rgb(243, 181, 103)", category: 'function', defaults: { name: {value:"Render"}, field: {value:"payload", validate: RED.validators.typedInput("fieldType")}, fieldType: {value:"msg"}, output: {value:"str"} }, inputs:1, outputs:1, icon: "template.png", label: function() { return this.name; }, oneditprepare: function() { var that = this; if (!this.fieldType) { this.fieldType = 'msg'; } $("#node-input-field").typedInput({ default: 'msg', types: ['msg','flow','global'], typeField: $("#node-input-fieldType") }); } }); </script>