|
|
@@ -444,84 +444,91 @@ |
|
|
Writer.prototype.renderTokens = function (tokens, context, partials, originalTemplate) { |
|
|
Writer.prototype.renderTokens = function (tokens, context, partials, originalTemplate) { |
|
|
var buffer = ''; |
|
|
var buffer = ''; |
|
|
|
|
|
|
|
|
// This function is used to render an arbitrary template |
|
|
|
|
|
// in the current context by higher-order sections. |
|
|
|
|
|
var self = this; |
|
|
|
|
|
function subRender(template) { |
|
|
|
|
|
return self.render(template, context, partials); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var token, value; |
|
|
|
|
|
|
|
|
var token, symbol, value; |
|
|
for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) { |
|
|
for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) { |
|
|
|
|
|
value = undefined; |
|
|
token = tokens[i]; |
|
|
token = tokens[i]; |
|
|
|
|
|
symbol = token[0]; |
|
|
|
|
|
|
|
|
switch (token[0]) { |
|
|
|
|
|
case '#': |
|
|
|
|
|
value = context.lookup(token[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 = this._unescapedValue(token, context); |
|
|
|
|
|
else if (symbol === 'name') value = this._escapedValue(token, context); |
|
|
|
|
|
else if (symbol === 'text') value = this._rawValue(token); |
|
|
|
|
|
|
|
|
if (!value) |
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
if (value !== undefined) |
|
|
|
|
|
buffer += value; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (isArray(value)) { |
|
|
|
|
|
for (var j = 0, valueLength = value.length; j < valueLength; ++j) { |
|
|
|
|
|
buffer += this.renderTokens(token[4], context.push(value[j]), partials, originalTemplate); |
|
|
|
|
|
} |
|
|
|
|
|
} else if (typeof value === 'object' || typeof value === 'string') { |
|
|
|
|
|
buffer += this.renderTokens(token[4], context.push(value), partials, originalTemplate); |
|
|
|
|
|
} else if (isFunction(value)) { |
|
|
|
|
|
if (typeof originalTemplate !== 'string') |
|
|
|
|
|
throw new Error('Cannot use higher-order sections without the original template'); |
|
|
|
|
|
|
|
|
return buffer; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
// Extract the portion of the original template that the section contains. |
|
|
|
|
|
value = value.call(context.view, originalTemplate.slice(token[3], token[5]), subRender); |
|
|
|
|
|
|
|
|
Writer.prototype._renderSection = function (token, context, partials, originalTemplate) { |
|
|
|
|
|
var self = this; |
|
|
|
|
|
var buffer = ''; |
|
|
|
|
|
var value = context.lookup(token[1]); |
|
|
|
|
|
|
|
|
if (value != null) |
|
|
|
|
|
buffer += value; |
|
|
|
|
|
} else { |
|
|
|
|
|
buffer += this.renderTokens(token[4], context, partials, originalTemplate); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// This function is used to render an arbitrary template |
|
|
|
|
|
// in the current context by higher-order sections. |
|
|
|
|
|
function subRender(template) { |
|
|
|
|
|
return self.render(template, context, partials); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
break; |
|
|
|
|
|
case '^': |
|
|
|
|
|
value = context.lookup(token[1]); |
|
|
|
|
|
|
|
|
if (!value) return; |
|
|
|
|
|
|
|
|
// Use JavaScript's definition of falsy. Include empty arrays. |
|
|
|
|
|
// See https://github.com/janl/mustache.js/issues/186 |
|
|
|
|
|
if (!value || (isArray(value) && value.length === 0)) |
|
|
|
|
|
buffer += this.renderTokens(token[4], context, partials, originalTemplate); |
|
|
|
|
|
|
|
|
if (isArray(value)) { |
|
|
|
|
|
for (var j = 0, valueLength = value.length; j < valueLength; ++j) { |
|
|
|
|
|
buffer += this.renderTokens(token[4], context.push(value[j]), partials, originalTemplate); |
|
|
|
|
|
} |
|
|
|
|
|
} else if (typeof value === 'object' || typeof value === 'string') { |
|
|
|
|
|
buffer += this.renderTokens(token[4], context.push(value), partials, originalTemplate); |
|
|
|
|
|
} else if (isFunction(value)) { |
|
|
|
|
|
if (typeof originalTemplate !== 'string') |
|
|
|
|
|
throw new Error('Cannot use higher-order sections without the original template'); |
|
|
|
|
|
|
|
|
break; |
|
|
|
|
|
case '>': |
|
|
|
|
|
if (!partials) |
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
// Extract the portion of the original template that the section contains. |
|
|
|
|
|
value = value.call(context.view, originalTemplate.slice(token[3], token[5]), subRender); |
|
|
|
|
|
|
|
|
value = isFunction(partials) ? partials(token[1]) : partials[token[1]]; |
|
|
|
|
|
|
|
|
if (value != null) |
|
|
|
|
|
buffer += value; |
|
|
|
|
|
} else { |
|
|
|
|
|
buffer += this.renderTokens(token[4], context, partials, originalTemplate); |
|
|
|
|
|
} |
|
|
|
|
|
return buffer; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
if (value != null) |
|
|
|
|
|
buffer += this.renderTokens(this.parse(value), context, partials, value); |
|
|
|
|
|
|
|
|
Writer.prototype._renderInverted = function(token, context, partials, originalTemplate) { |
|
|
|
|
|
var value = context.lookup(token[1]); |
|
|
|
|
|
|
|
|
break; |
|
|
|
|
|
case '&': |
|
|
|
|
|
value = context.lookup(token[1]); |
|
|
|
|
|
|
|
|
// Use JavaScript's definition of falsy. Include empty arrays. |
|
|
|
|
|
// See https://github.com/janl/mustache.js/issues/186 |
|
|
|
|
|
if (!value || (isArray(value) && value.length === 0)) |
|
|
|
|
|
return this.renderTokens(token[4], context, partials, originalTemplate); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
if (value != null) |
|
|
|
|
|
buffer += value; |
|
|
|
|
|
|
|
|
Writer.prototype._renderPartial = function(token, context, partials, originalTemplate) { |
|
|
|
|
|
if (!partials) return; |
|
|
|
|
|
|
|
|
break; |
|
|
|
|
|
case 'name': |
|
|
|
|
|
value = context.lookup(token[1]); |
|
|
|
|
|
|
|
|
var value = isFunction(partials) ? partials(token[1]) : partials[token[1]]; |
|
|
|
|
|
if (value != null) |
|
|
|
|
|
return this.renderTokens(this.parse(value), context, partials, value); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
if (value != null) |
|
|
|
|
|
buffer += mustache.escape(value); |
|
|
|
|
|
|
|
|
Writer.prototype._unescapedValue = function(token, context) { |
|
|
|
|
|
var value = context.lookup(token[1]); |
|
|
|
|
|
if (value != null) |
|
|
|
|
|
return value; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
break; |
|
|
|
|
|
case 'text': |
|
|
|
|
|
buffer += token[1]; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
Writer.prototype._escapedValue = function(token, context) { |
|
|
|
|
|
var value = context.lookup(token[1]); |
|
|
|
|
|
if (value != null) |
|
|
|
|
|
return mustache.escape(value); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
return buffer; |
|
|
|
|
|
|
|
|
Writer.prototype._rawValue = function(token) { |
|
|
|
|
|
return token[1]; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
mustache.name = "mustache.js"; |
|
|
mustache.name = "mustache.js"; |
|
|
|