浏览代码

Preserve indentation when rendering partials

pull/703/head
Yotam Madem 6 年前
父节点
当前提交
337da204ee
共有 2 个文件被更改,包括 43 次插入6 次删除
  1. +35
    -6
      mustache.js
  2. +8
    -0
      test/render-test.js

+ 35
- 6
mustache.js 查看文件

@@ -528,6 +528,7 @@
*/
Writer.prototype.renderTokens = function renderTokens (tokens, context, partials, originalTemplate, tags) {
var buffer = '';
var indentationContext = {spacer: ''};

var token, symbol, value;
for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) {
@@ -537,18 +538,31 @@

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, tags);
else if (symbol === '>') value = this.renderPartial(token, context, partials, tags, indentationContext);
else if (symbol === '&') value = this.unescapedValue(token, context);
else if (symbol === 'name') value = this.escapedValue(token, context);
else if (symbol === 'text') value = this.rawValue(token);

if (value !== undefined)
if (value !== undefined) {
this.updateIndentationContext(indentationContext, value);
buffer += value;
}
}

return buffer;
};

Writer.prototype.updateIndentationContext = function updateIndentationContext (indentationContext, value) {
for (var j = 0; j < value.length; j++) {
if (value[j] == '\n') {
indentationContext.spacer = '';
} else if (isWhitespace(value[j])) {
indentationContext.spacer += value[j];
} else {
indentationContext.spacer += ' ';
}
}
};

Writer.prototype.renderSection = function renderSection (token, context, partials, originalTemplate) {
var self = this;
var buffer = '';
@@ -592,12 +606,27 @@
return this.renderTokens(token[4], context, partials, originalTemplate);
};

Writer.prototype.renderPartial = function renderPartial (token, context, partials, tags) {
Writer.prototype.renderPartial = function renderPartial (token, context, partials, tags, indentationContext) {
if (!partials) return;

var value = isFunction(partials) ? partials(token[1]) : partials[token[1]];
if (value != null)
return this.renderTokens(this.parse(value, tags), context, partials, value);
if (value != null) {
var renderResult = this.renderTokens(this.parse(value, tags), context, partials, value);
return this.indent(renderResult, indentationContext);
}
};

Writer.prototype.indent = function indent (value, indentationContext) {
var indentedValue = '';
var lines = value.split('\n');
for (var i=0; i<lines.length; i++) {
if (i == 0) {
indentedValue += lines[i];
} else {
indentedValue += ('\n' + indentationContext.spacer + lines[i]);
}
}
return indentedValue;
};

Writer.prototype.unescapedValue = function unescapedValue (token, context) {


+ 8
- 0
test/render-test.js 查看文件

@@ -17,6 +17,14 @@ describe('Mustache.render', function () {
'for mustache#render(template, view, partials)');
});

describe('preserve indentation when using partials', function() {
it.only ('should preserve indentation', function() {
var template = 'line1\n bla la \t\r\f foo line2{{>p1}}';
var renderResult = Mustache.render(template, {}, {p1: 'l1\nl2'});
assert.equal(renderResult, 'line1\n bla la \t\r\f foo line2l1\n \t\r\f l2');
});
});

describe('custom tags', function () {
it('uses tags argument instead of Mustache.tags when given', function () {
var template = '<<placeholder>>bar{{placeholder}}';


正在加载...
取消
保存