1
0

mustache.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*!
  2. * mustache.js - Logic-less {{mustache}} templates with JavaScript
  3. * http://github.com/janl/mustache.js
  4. */
  5. /*global define: false Mustache: true*/
  6. (function defineMustache (global, factory) {
  7. if (typeof exports === 'object' && exports && typeof exports.nodeName !== 'string') {
  8. factory(exports); // CommonJS
  9. } else if (typeof define === 'function' && define.amd) {
  10. define(['exports'], factory); // AMD
  11. } else {
  12. global.Mustache = {};
  13. factory(global.Mustache); // script, wsh, asp
  14. }
  15. }(this, function mustacheFactory (mustache) {
  16. var objectToString = Object.prototype.toString;
  17. var isArray = Array.isArray || function isArrayPolyfill (object) {
  18. return objectToString.call(object) === '[object Array]';
  19. };
  20. function isFunction (object) {
  21. return typeof object === 'function';
  22. }
  23. /**
  24. * More correct typeof string handling array
  25. * which normally returns typeof 'object'
  26. */
  27. function typeStr (obj) {
  28. return isArray(obj) ? 'array' : typeof obj;
  29. }
  30. function escapeRegExp (string) {
  31. return string.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&');
  32. }
  33. /**
  34. * Null safe way of checking whether or not an object,
  35. * including its prototype, has a given property
  36. */
  37. function hasProperty (obj, propName) {
  38. return obj != null && typeof obj === 'object' && (propName in obj);
  39. }
  40. /**
  41. * Safe way of detecting whether or not the given thing is a primitive and
  42. * whether it has the given property
  43. */
  44. function primitiveHasOwnProperty (primitive, propName) {
  45. return (
  46. primitive != null
  47. && typeof primitive !== 'object'
  48. && primitive.hasOwnProperty
  49. && primitive.hasOwnProperty(propName)
  50. );
  51. }
  52. // Workaround for https://issues.apache.org/jira/browse/COUCHDB-577
  53. // See https://github.com/janl/mustache.js/issues/189
  54. var regExpTest = RegExp.prototype.test;
  55. function testRegExp (re, string) {
  56. return regExpTest.call(re, string);
  57. }
  58. var nonSpaceRe = /\S/;
  59. function isWhitespace (string) {
  60. return !testRegExp(nonSpaceRe, string);
  61. }
  62. var entityMap = {
  63. '&': '&',
  64. '<': '&lt;',
  65. '>': '&gt;',
  66. '"': '&quot;',
  67. "'": '&#39;',
  68. '/': '&#x2F;',
  69. '`': '&#x60;',
  70. '=': '&#x3D;'
  71. };
  72. function escapeHtml (string) {
  73. return String(string).replace(/[&<>"'`=\/]/g, function fromEntityMap (s) {
  74. return entityMap[s];
  75. });
  76. }
  77. var whiteRe = /\s*/;
  78. var spaceRe = /\s+/;
  79. var equalsRe = /\s*=/;
  80. var curlyRe = /\s*\}/;
  81. var tagRe = /#|\^|\/|>|\{|&|=|!/;
  82. /**
  83. * Breaks up the given `template` string into a tree of tokens. If the `tags`
  84. * argument is given here it must be an array with two string values: the
  85. * opening and closing tags used in the template (e.g. [ "<%", "%>" ]). Of
  86. * course, the default is to use mustaches (i.e. mustache.tags).
  87. *
  88. * A token is an array with at least 4 elements. The first element is the
  89. * mustache symbol that was used inside the tag, e.g. "#" or "&". If the tag
  90. * did not contain a symbol (i.e. {{myValue}}) this element is "name". For
  91. * all text that appears outside a symbol this element is "text".
  92. *
  93. * The second element of a token is its "value". For mustache tags this is
  94. * whatever else was inside the tag besides the opening symbol. For text tokens
  95. * this is the text itself.
  96. *
  97. * The third and fourth elements of the token are the start and end indices,
  98. * respectively, of the token in the original template.
  99. *
  100. * Tokens that are the root node of a subtree contain two more elements: 1) an
  101. * array of tokens in the subtree and 2) the index in the original template at
  102. * which the closing tag for that section begins.
  103. *
  104. * Tokens for partials also contain two more elements: 1) a string value of
  105. * indendation prior to that tag and 2) the index of that tag on that line -
  106. * eg a value of 2 indicates the partial is the third tag on this line.
  107. */
  108. function parseTemplate (template, tags) {
  109. if (!template)
  110. return [];
  111. var lineHasNonSpace = false;
  112. var sections = []; // Stack to hold section tokens
  113. var tokens = []; // Buffer to hold the tokens
  114. var spaces = []; // Indices of whitespace tokens on the current line
  115. var hasTag = false; // Is there a {{tag}} on the current line?
  116. var nonSpace = false; // Is there a non-space char on the current line?
  117. var indentation = ''; // Tracks indentation for tags that use it
  118. var tagIndex = 0; // Stores a count of number of tags encountered on a line
  119. // Strips all whitespace tokens array for the current line
  120. // if there was a {{#tag}} on it and otherwise only space.
  121. function stripSpace () {
  122. if (hasTag && !nonSpace) {
  123. while (spaces.length)
  124. delete tokens[spaces.pop()];
  125. } else {
  126. spaces = [];
  127. }
  128. hasTag = false;
  129. nonSpace = false;
  130. }
  131. var openingTagRe, closingTagRe, closingCurlyRe;
  132. function compileTags (tagsToCompile) {
  133. if (typeof tagsToCompile === 'string')
  134. tagsToCompile = tagsToCompile.split(spaceRe, 2);
  135. if (!isArray(tagsToCompile) || tagsToCompile.length !== 2)
  136. throw new Error('Invalid tags: ' + tagsToCompile);
  137. openingTagRe = new RegExp(escapeRegExp(tagsToCompile[0]) + '\\s*');
  138. closingTagRe = new RegExp('\\s*' + escapeRegExp(tagsToCompile[1]));
  139. closingCurlyRe = new RegExp('\\s*' + escapeRegExp('}' + tagsToCompile[1]));
  140. }
  141. compileTags(tags || mustache.tags);
  142. var scanner = new Scanner(template);
  143. var start, type, value, chr, token, openSection;
  144. while (!scanner.eos()) {
  145. start = scanner.pos;
  146. // Match any text between tags.
  147. value = scanner.scanUntil(openingTagRe);
  148. if (value) {
  149. for (var i = 0, valueLength = value.length; i < valueLength; ++i) {
  150. chr = value.charAt(i);
  151. if (isWhitespace(chr)) {
  152. spaces.push(tokens.length);
  153. indentation += chr;
  154. } else {
  155. nonSpace = true;
  156. lineHasNonSpace = true;
  157. indentation += ' ';
  158. }
  159. tokens.push([ 'text', chr, start, start + 1 ]);
  160. start += 1;
  161. // Check for whitespace on the current line.
  162. if (chr === '\n') {
  163. stripSpace();
  164. indentation = '';
  165. tagIndex = 0;
  166. lineHasNonSpace = false;
  167. }
  168. }
  169. }
  170. // Match the opening tag.
  171. if (!scanner.scan(openingTagRe))
  172. break;
  173. hasTag = true;
  174. // Get the tag type.
  175. type = scanner.scan(tagRe) || 'name';
  176. scanner.scan(whiteRe);
  177. // Get the tag value.
  178. if (type === '=') {
  179. value = scanner.scanUntil(equalsRe);
  180. scanner.scan(equalsRe);
  181. scanner.scanUntil(closingTagRe);
  182. } else if (type === '{') {
  183. value = scanner.scanUntil(closingCurlyRe);
  184. scanner.scan(curlyRe);
  185. scanner.scanUntil(closingTagRe);
  186. type = '&';
  187. } else {
  188. value = scanner.scanUntil(closingTagRe);
  189. }
  190. // Match the closing tag.
  191. if (!scanner.scan(closingTagRe))
  192. throw new Error('Unclosed tag at ' + scanner.pos);
  193. if (type == '>') {
  194. token = [ type, value, start, scanner.pos, indentation, tagIndex, lineHasNonSpace ];
  195. } else {
  196. token = [ type, value, start, scanner.pos ];
  197. }
  198. tagIndex++;
  199. tokens.push(token);
  200. if (type === '#' || type === '^') {
  201. sections.push(token);
  202. } else if (type === '/') {
  203. // Check section nesting.
  204. openSection = sections.pop();
  205. if (!openSection)
  206. throw new Error('Unopened section "' + value + '" at ' + start);
  207. if (openSection[1] !== value)
  208. throw new Error('Unclosed section "' + openSection[1] + '" at ' + start);
  209. } else if (type === 'name' || type === '{' || type === '&') {
  210. nonSpace = true;
  211. } else if (type === '=') {
  212. // Set the tags for the next time around.
  213. compileTags(value);
  214. }
  215. }
  216. stripSpace();
  217. // Make sure there are no open sections when we're done.
  218. openSection = sections.pop();
  219. if (openSection)
  220. throw new Error('Unclosed section "' + openSection[1] + '" at ' + scanner.pos);
  221. return nestTokens(squashTokens(tokens));
  222. }
  223. /**
  224. * Combines the values of consecutive text tokens in the given `tokens` array
  225. * to a single token.
  226. */
  227. function squashTokens (tokens) {
  228. var squashedTokens = [];
  229. var token, lastToken;
  230. for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) {
  231. token = tokens[i];
  232. if (token) {
  233. if (token[0] === 'text' && lastToken && lastToken[0] === 'text') {
  234. lastToken[1] += token[1];
  235. lastToken[3] = token[3];
  236. } else {
  237. squashedTokens.push(token);
  238. lastToken = token;
  239. }
  240. }
  241. }
  242. return squashedTokens;
  243. }
  244. /**
  245. * Forms the given array of `tokens` into a nested tree structure where
  246. * tokens that represent a section have two additional items: 1) an array of
  247. * all tokens that appear in that section and 2) the index in the original
  248. * template that represents the end of that section.
  249. */
  250. function nestTokens (tokens) {
  251. var nestedTokens = [];
  252. var collector = nestedTokens;
  253. var sections = [];
  254. var token, section;
  255. for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) {
  256. token = tokens[i];
  257. switch (token[0]) {
  258. case '#':
  259. case '^':
  260. collector.push(token);
  261. sections.push(token);
  262. collector = token[4] = [];
  263. break;
  264. case '/':
  265. section = sections.pop();
  266. section[5] = token[2];
  267. collector = sections.length > 0 ? sections[sections.length - 1][4] : nestedTokens;
  268. break;
  269. default:
  270. collector.push(token);
  271. }
  272. }
  273. return nestedTokens;
  274. }
  275. /**
  276. * A simple string scanner that is used by the template parser to find
  277. * tokens in template strings.
  278. */
  279. function Scanner (string) {
  280. this.string = string;
  281. this.tail = string;
  282. this.pos = 0;
  283. }
  284. /**
  285. * Returns `true` if the tail is empty (end of string).
  286. */
  287. Scanner.prototype.eos = function eos () {
  288. return this.tail === '';
  289. };
  290. /**
  291. * Tries to match the given regular expression at the current position.
  292. * Returns the matched text if it can match, the empty string otherwise.
  293. */
  294. Scanner.prototype.scan = function scan (re) {
  295. var match = this.tail.match(re);
  296. if (!match || match.index !== 0)
  297. return '';
  298. var string = match[0];
  299. this.tail = this.tail.substring(string.length);
  300. this.pos += string.length;
  301. return string;
  302. };
  303. /**
  304. * Skips all text until the given regular expression can be matched. Returns
  305. * the skipped string, which is the entire tail if no match can be made.
  306. */
  307. Scanner.prototype.scanUntil = function scanUntil (re) {
  308. var index = this.tail.search(re), match;
  309. switch (index) {
  310. case -1:
  311. match = this.tail;
  312. this.tail = '';
  313. break;
  314. case 0:
  315. match = '';
  316. break;
  317. default:
  318. match = this.tail.substring(0, index);
  319. this.tail = this.tail.substring(index);
  320. }
  321. this.pos += match.length;
  322. return match;
  323. };
  324. /**
  325. * Represents a rendering context by wrapping a view object and
  326. * maintaining a reference to the parent context.
  327. */
  328. function Context (view, parentContext) {
  329. this.view = view;
  330. this.cache = { '.': this.view };
  331. this.parent = parentContext;
  332. }
  333. /**
  334. * Creates a new context using the given view with this context
  335. * as the parent.
  336. */
  337. Context.prototype.push = function push (view) {
  338. return new Context(view, this);
  339. };
  340. /**
  341. * Returns the value of the given name in this context, traversing
  342. * up the context hierarchy if the value is absent in this context's view.
  343. */
  344. Context.prototype.lookup = function lookup (name) {
  345. var cache = this.cache;
  346. var value;
  347. if (cache.hasOwnProperty(name)) {
  348. value = cache[name];
  349. } else {
  350. var context = this, intermediateValue, names, index, lookupHit = false;
  351. while (context) {
  352. if (name.indexOf('.') > 0) {
  353. intermediateValue = context.view;
  354. names = name.split('.');
  355. index = 0;
  356. /**
  357. * Using the dot notion path in `name`, we descend through the
  358. * nested objects.
  359. *
  360. * To be certain that the lookup has been successful, we have to
  361. * check if the last object in the path actually has the property
  362. * we are looking for. We store the result in `lookupHit`.
  363. *
  364. * This is specially necessary for when the value has been set to
  365. * `undefined` and we want to avoid looking up parent contexts.
  366. *
  367. * In the case where dot notation is used, we consider the lookup
  368. * to be successful even if the last "object" in the path is
  369. * not actually an object but a primitive (e.g., a string, or an
  370. * integer), because it is sometimes useful to access a property
  371. * of an autoboxed primitive, such as the length of a string.
  372. **/
  373. while (intermediateValue != null && index < names.length) {
  374. if (index === names.length - 1)
  375. lookupHit = (
  376. hasProperty(intermediateValue, names[index])
  377. || primitiveHasOwnProperty(intermediateValue, names[index])
  378. );
  379. intermediateValue = intermediateValue[names[index++]];
  380. }
  381. } else {
  382. intermediateValue = context.view[name];
  383. /**
  384. * Only checking against `hasProperty`, which always returns `false` if
  385. * `context.view` is not an object. Deliberately omitting the check
  386. * against `primitiveHasOwnProperty` if dot notation is not used.
  387. *
  388. * Consider this example:
  389. * ```
  390. * Mustache.render("The length of a football field is {{#length}}{{length}}{{/length}}.", {length: "100 yards"})
  391. * ```
  392. *
  393. * If we were to check also against `primitiveHasOwnProperty`, as we do
  394. * in the dot notation case, then render call would return:
  395. *
  396. * "The length of a football field is 9."
  397. *
  398. * rather than the expected:
  399. *
  400. * "The length of a football field is 100 yards."
  401. **/
  402. lookupHit = hasProperty(context.view, name);
  403. }
  404. if (lookupHit) {
  405. value = intermediateValue;
  406. break;
  407. }
  408. context = context.parent;
  409. }
  410. cache[name] = value;
  411. }
  412. if (isFunction(value))
  413. value = value.call(this.view);
  414. return value;
  415. };
  416. /**
  417. * A Writer knows how to take a stream of tokens and render them to a
  418. * string, given a context. It also maintains a cache of templates to
  419. * avoid the need to parse the same template twice.
  420. */
  421. function Writer () {
  422. this.cache = {};
  423. }
  424. /**
  425. * Clears all cached templates in this writer.
  426. */
  427. Writer.prototype.clearCache = function clearCache () {
  428. this.cache = {};
  429. };
  430. /**
  431. * Parses and caches the given `template` according to the given `tags` or
  432. * `mustache.tags` if `tags` is omitted, and returns the array of tokens
  433. * that is generated from the parse.
  434. */
  435. Writer.prototype.parse = function parse (template, tags) {
  436. var cache = this.cache;
  437. var cacheKey = template + ':' + (tags || mustache.tags).join(':');
  438. var tokens = cache[cacheKey];
  439. if (tokens == null)
  440. tokens = cache[cacheKey] = parseTemplate(template, tags);
  441. return tokens;
  442. };
  443. /**
  444. * High-level method that is used to render the given `template` with
  445. * the given `view`.
  446. *
  447. * The optional `partials` argument may be an object that contains the
  448. * names and templates of partials that are used in the template. It may
  449. * also be a function that is used to load partial templates on the fly
  450. * that takes a single argument: the name of the partial.
  451. *
  452. * If the optional `tags` argument is given here it must be an array with two
  453. * string values: the opening and closing tags used in the template (e.g.
  454. * [ "<%", "%>" ]). The default is to mustache.tags.
  455. */
  456. Writer.prototype.render = function render (template, view, partials, tags) {
  457. var tokens = this.parse(template, tags);
  458. var context = (view instanceof Context) ? view : new Context(view);
  459. return this.renderTokens(tokens, context, partials, template, tags);
  460. };
  461. /**
  462. * Low-level method that renders the given array of `tokens` using
  463. * the given `context` and `partials`.
  464. *
  465. * Note: The `originalTemplate` is only ever used to extract the portion
  466. * of the original template that was contained in a higher-order section.
  467. * If the template doesn't use higher-order sections, this argument may
  468. * be omitted.
  469. */
  470. Writer.prototype.renderTokens = function renderTokens (tokens, context, partials, originalTemplate, tags) {
  471. var buffer = '';
  472. var token, symbol, value;
  473. for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) {
  474. value = undefined;
  475. token = tokens[i];
  476. symbol = token[0];
  477. if (symbol === '#') value = this.renderSection(token, context, partials, originalTemplate);
  478. else if (symbol === '^') value = this.renderInverted(token, context, partials, originalTemplate);
  479. else if (symbol === '>') value = this.renderPartial(token, context, partials, tags);
  480. else if (symbol === '&') value = this.unescapedValue(token, context);
  481. else if (symbol === 'name') value = this.escapedValue(token, context);
  482. else if (symbol === 'text') value = this.rawValue(token);
  483. if (value !== undefined)
  484. buffer += value;
  485. }
  486. return buffer;
  487. };
  488. Writer.prototype.renderSection = function renderSection (token, context, partials, originalTemplate) {
  489. var self = this;
  490. var buffer = '';
  491. var value = context.lookup(token[1]);
  492. // This function is used to render an arbitrary template
  493. // in the current context by higher-order sections.
  494. function subRender (template) {
  495. return self.render(template, context, partials);
  496. }
  497. if (!value) return;
  498. if (isArray(value)) {
  499. for (var j = 0, valueLength = value.length; j < valueLength; ++j) {
  500. buffer += this.renderTokens(token[4], context.push(value[j]), partials, originalTemplate);
  501. }
  502. } else if (typeof value === 'object' || typeof value === 'string' || typeof value === 'number') {
  503. buffer += this.renderTokens(token[4], context.push(value), partials, originalTemplate);
  504. } else if (isFunction(value)) {
  505. if (typeof originalTemplate !== 'string')
  506. throw new Error('Cannot use higher-order sections without the original template');
  507. // Extract the portion of the original template that the section contains.
  508. value = value.call(context.view, originalTemplate.slice(token[3], token[5]), subRender);
  509. if (value != null)
  510. buffer += value;
  511. } else {
  512. buffer += this.renderTokens(token[4], context, partials, originalTemplate);
  513. }
  514. return buffer;
  515. };
  516. Writer.prototype.renderInverted = function renderInverted (token, context, partials, originalTemplate) {
  517. var value = context.lookup(token[1]);
  518. // Use JavaScript's definition of falsy. Include empty arrays.
  519. // See https://github.com/janl/mustache.js/issues/186
  520. if (!value || (isArray(value) && value.length === 0))
  521. return this.renderTokens(token[4], context, partials, originalTemplate);
  522. };
  523. Writer.prototype.indentPartial = function indentPartial (partial, indentation, lineHasNonSpace) {
  524. var filteredIndentation = indentation.replace(/[^ \t]/g, '');
  525. var partialByNl = partial.split('\n');
  526. for (var i = 0; i < partialByNl.length; i++) {
  527. if (partialByNl[i].length && (i > 0 || !lineHasNonSpace)) {
  528. partialByNl[i] = filteredIndentation + partialByNl[i];
  529. }
  530. }
  531. return partialByNl.join('\n');
  532. };
  533. Writer.prototype.renderPartial = function renderPartial (token, context, partials, tags) {
  534. if (!partials) return;
  535. var value = isFunction(partials) ? partials(token[1]) : partials[token[1]];
  536. if (value != null) {
  537. var lineHasNonSpace = token[6];
  538. var tagIndex = token[5];
  539. var indentation = token[4];
  540. var indentedValue = value;
  541. if (tagIndex == 0 && indentation) {
  542. indentedValue = this.indentPartial(value, indentation, lineHasNonSpace);
  543. }
  544. return this.renderTokens(this.parse(indentedValue, tags), context, partials, indentedValue);
  545. }
  546. };
  547. Writer.prototype.unescapedValue = function unescapedValue (token, context) {
  548. var value = context.lookup(token[1]);
  549. if (value != null)
  550. return value;
  551. };
  552. Writer.prototype.escapedValue = function escapedValue (token, context) {
  553. var value = context.lookup(token[1]);
  554. if (value != null)
  555. return mustache.escape(value);
  556. };
  557. Writer.prototype.rawValue = function rawValue (token) {
  558. return token[1];
  559. };
  560. mustache.name = 'mustache.js';
  561. mustache.version = '3.1.0';
  562. mustache.tags = [ '{{', '}}' ];
  563. // All high-level mustache.* functions use this writer.
  564. var defaultWriter = new Writer();
  565. /**
  566. * Clears all cached templates in the default writer.
  567. */
  568. mustache.clearCache = function clearCache () {
  569. return defaultWriter.clearCache();
  570. };
  571. /**
  572. * Parses and caches the given template in the default writer and returns the
  573. * array of tokens it contains. Doing this ahead of time avoids the need to
  574. * parse templates on the fly as they are rendered.
  575. */
  576. mustache.parse = function parse (template, tags) {
  577. return defaultWriter.parse(template, tags);
  578. };
  579. /**
  580. * Renders the `template` with the given `view` and `partials` using the
  581. * default writer. If the optional `tags` argument is given here it must be an
  582. * array with two string values: the opening and closing tags used in the
  583. * template (e.g. [ "<%", "%>" ]). The default is to mustache.tags.
  584. */
  585. mustache.render = function render (template, view, partials, tags) {
  586. if (typeof template !== 'string') {
  587. throw new TypeError('Invalid template! Template should be a "string" ' +
  588. 'but "' + typeStr(template) + '" was given as the first ' +
  589. 'argument for mustache#render(template, view, partials)');
  590. }
  591. return defaultWriter.render(template, view, partials, tags);
  592. };
  593. // This is here for backwards compatibility with 0.4.x.,
  594. /*eslint-disable */ // eslint wants camel cased function name
  595. mustache.to_html = function to_html (template, view, partials, send) {
  596. /*eslint-enable*/
  597. var result = mustache.render(template, view, partials);
  598. if (isFunction(send)) {
  599. send(result);
  600. } else {
  601. return result;
  602. }
  603. };
  604. // Export the escaping function so that the user may override it.
  605. // See https://github.com/janl/mustache.js/issues/244
  606. mustache.escape = escapeHtml;
  607. // Export these mainly for testing, but also for advanced usage.
  608. mustache.Scanner = Scanner;
  609. mustache.Context = Context;
  610. mustache.Writer = Writer;
  611. return mustache;
  612. }));