How to use the mustache.Context function in mustache

To help you get started, we’ve selected a few mustache examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zachowj / node-red-contrib-home-assistant-websocket / lib / mustache-context.js View on Github external
}
    return undefined;
}

/**
 * Custom Mustache Context capable to collect message property and node
 * flow and global context
 */

function NodeContext(msg, parent, nodeContext, serverName) {
    this.msgContext = new mustache.Context(msg, parent);
    this.nodeContext = nodeContext;
    this.serverName = serverName;
}

NodeContext.prototype = new mustache.Context();

NodeContext.prototype.lookup = function(name) {
    // try message first:
    const value = this.msgContext.lookup(name);
    if (value !== undefined) {
        return value;
    }

    // try flow/global context:
    const context = parseContext(name);
    if (context) {
        const type = context.type;
        const store = context.store;
        const field = context.field;
        const target = this.nodeContext[type];
        if (target) {
github node-red / node-red / packages / node_modules / @node-red / nodes / core / function / 80-template.js View on Github external
return undefined;
    }

    /**
     * Custom Mustache Context capable to collect message property and node
     * flow and global context
     */

    function NodeContext(msg, nodeContext, parent, escapeStrings, cachedContextTokens) {
        this.msgContext = new mustache.Context(msg,parent);
        this.nodeContext = nodeContext;
        this.escapeStrings = escapeStrings;
        this.cachedContextTokens = cachedContextTokens;
    }

    NodeContext.prototype = new mustache.Context();

    NodeContext.prototype.lookup = function (name) {
        // try message first:
        try {
            var value = this.msgContext.lookup(name);
            if (value !== undefined) {
                if (this.escapeStrings && typeof value === "string") {
                    value = value.replace(/\\/g, "\\\\");
                    value = value.replace(/\n/g, "\\n");
                    value = value.replace(/\t/g, "\\t");
                    value = value.replace(/\r/g, "\\r");
                    value = value.replace(/\f/g, "\\f");
                    value = value.replace(/[\b]/g, "\\b");
                }
                return value;
            }
github electrode-io / electrode-native / ern-api-gen / src / java / Mustache.ts View on Github external
import Mustache from 'mustache'
import Json from './Json'
import { log } from 'ern-core'
import { MustacheWriter } from './MustacheWriter'
import { isIterable } from './isIterable'

function truthy(val) {
  if (val == null || val.length === 0 || val === false) {
    return false
  }
  return true
}
function isInt(val) {
  return /^\d+?$/.test(val)
}
class MyContext extends Mustache.Context {
  protected _first: any
  protected _last: any

  constructor(value, parent?: any, first?: any, last?: any) {
    super(value, parent)
    this._first = first
    this._last = last
  }

  public lookup(name) {
    if (/-?last/.test(name)) {
      return this._last
    }
    if (/-?first/.test(name)) {
      return this._first
    }
github microsoft / pai / src / rest-server / src / middlewares / v2 / protocol.js View on Github external
const render = (template, dict, tags=['<%', '%>']) => {
  const tokens = mustacheWriter.parse(template, tags);
  const context = new mustache.Context(dict);
  let result = '';
  for (let token of tokens) {
    const symbol = token[0];
    let tokenStr = token[1];
    if (symbol === 'text') {
      result += tokenStr;
    } else if (symbol === 'name') {
      tokenStr = tokenStr.replace(/\[(\d+)\]/g, '.$1');
      const value = context.lookup(tokenStr);
      if (value != null) {
        result += value;
      }
    }
  }
  return result.trim();
};
github userpixel / micromustache / perf / cases / mustache.js View on Github external
function compiled(obj) {
  return writer.renderTokens(mustacheCompileTokens, new mustache.Context(obj))
}
github userpixel / micromustache / examples / perf.js View on Github external
function mustache_compile(obj) {
  return mustache_writer.renderTokens(mustache_compile_tokens, new mustache.Context(obj))
}
github zachowj / node-red-contrib-home-assistant-websocket / lib / mustache-context.js View on Github external
function NodeContext(msg, parent, nodeContext, serverName) {
    this.msgContext = new mustache.Context(msg, parent);
    this.nodeContext = nodeContext;
    this.serverName = serverName;
}
github node-red / node-red / packages / node_modules / @node-red / nodes / core / function / 80-template.js View on Github external
function NodeContext(msg, nodeContext, parent, escapeStrings, cachedContextTokens) {
        this.msgContext = new mustache.Context(msg,parent);
        this.nodeContext = nodeContext;
        this.escapeStrings = escapeStrings;
        this.cachedContextTokens = cachedContextTokens;
    }