有没有办法只渲染部分?

时间:2015-04-18 00:30:08

标签: mustache

有没有办法只渲染部分?

例如,header包含的footer部分和home.mustache部分将生成home-all.mustache

1 个答案:

答案 0 :(得分:0)

是的!只需更改prototype

即可
var Mustache = require('mustache');

Mustache.Writer.prototype.renderTokens = function (tokens, context, partials, originalTemplate) {
  var buffer = '';

  var token, symbol, value, tag;
  for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) {
    value = undefined;
    token = tokens[i];
    symbol = token[0];
    tag = value = mustache.tags[0] + token[1] + mustache.tags[1];

    if (symbol === '#') value = this._renderSection(token, context, partials, originalTemplate);
    else if (symbol === '^') value = this._renderInverted(token, context, partials, originalTemplate);
    else if (symbol === '>') value = this._renderPartial(token, context, partials, originalTemplate);
    else if (symbol === '&') value = tag;
    else if (symbol === 'name') value = tag;
    else if (symbol === 'text') value = this._rawValue(token);

    if (value !== undefined)
      buffer += value;
  }

  return buffer;
};

现在它将输出只有部分解析为一个模板。