Sfoglia il codice sorgente

Make high-level API same as Renderer API

Also, allow partials to be passed to the function that is returned
for pre-compiled templates. This makes the API more consistent all
around, but comes with the caveat that renderers are much more
aggressive at caching. Now, any time a template or partial is
compiled with the same name as a previously compiled template the
cached version is used.

Fixes #240
tags/0.7.0
Michael Jackson 13 anni fa
parent
commit
544a32b7a1
2 ha cambiato i file con 35 aggiunte e 39 eliminazioni
  1. +2
    -0
      CHANGES
  2. +33
    -39
      mustache.js

+ 2
- 0
CHANGES Vedi File

@@ -3,6 +3,8 @@
* Fixed a bug with higher-order sections that prevented them from being
passed the raw text of the section from the original template.
* More concise token format.
* High-level API is consistent with the Renderer API.
* Allow partials to be passed to the pre-compiled function (thanks @fallenice).

= 0.6.0 / 31 Aug 2012



+ 33
- 39
mustache.js Vedi File

@@ -219,30 +219,36 @@ var Mustache;
this._partialCache = {};
};

Renderer.prototype.render = function (template, view) {
var fn = this._cache[template];
Renderer.prototype._compile = function (cache, key, template, tags) {
if (!cache[key]) {
var tokens = parse(template, tags);
var fn = compileTokens(tokens);

if (!fn) {
fn = this.compile(template);
this._cache[template] = fn;
var self = this;
cache[key] = function (view, partials) {
if (partials) {
for (var name in partials) {
self.compilePartial(name, partials[name]);
}
}

return fn(Context.make(view), self, template);
};
}

return fn(view);
return cache[key];
};

Renderer.prototype.compile = function (template, tags) {
var tokens = parse(template, tags);
var render = compileTokens(tokens);

var self = this;
return function (view) {
return render(Context.make(view), self, template);
};
return this._compile(this._cache, template, template, tags);
};

Renderer.prototype.compilePartial = function (name, template, tags) {
this._partialCache[name] = this.compile(template, tags);
return this._partialCache[name];
return this._compile(this._partialCache, name, template, tags);
};

Renderer.prototype.render = function (template, view, partials) {
return this.compile(template)(view, partials);
};

Renderer.prototype._section = function (name, context, text, callback) {
@@ -577,46 +583,34 @@ var Mustache;
var _renderer = new Renderer();

/**
* Clears all cached templates and partials.
* Clears all cached templates and partials in the default renderer.
*/
function clearCache() {
_renderer.clearCache();
}

/**
* High-level API for compiling the given `tokens` down to a reusable
* function. If `tokens` is a string it will be parsed using the given `tags`
* before it is compiled.
* Compiles the given `template` to a reusable function using the default
* renderer.
*/
function compile(tokens, tags) {
return _renderer.compile(tokens, tags);
function compile(template, tags) {
return _renderer.compile(template, tags);
}

/**
* High-level API for compiling the `tokens` for the partial with the given
* `name` down to a reusable function. If `tokens` is a string it will be
* parsed using the given `tags` before it is compiled.
* Compiles the partial with the given `name` and `template` to a reusable
* function using the default renderer.
*/
function compilePartial(name, tokens, tags) {
return _renderer.compilePartial(name, tokens, tags);
function compilePartial(name, template, tags) {
return _renderer.compilePartial(name, template, tags);
}

/**
* High-level API for rendering the `template` using the given `view`. The
* optional `partials` object may be given here for convenience, but note that
* it will cause all partials to be re-compiled, thus hurting performance. Of
* course, this only matters if you're going to render the same template more
* than once. If so, it is best to call `compilePartial` before calling this
* function and to leave the `partials` argument blank.
* Renders the `template` with the given `view` and `partials` using the
* default renderer.
*/
function render(template, view, partials) {
if (partials) {
for (var name in partials) {
compilePartial(name, partials[name]);
}
}

return _renderer.render(template, view);
return _renderer.render(template, view, partials);
}

return exports;


Loading…
Annulla
Salva