janl/mustache.js — Issue #180: Code by generated by parse relies on Mustache private functions
Author: brigham
Created: 2012-03-06 20:21:54+00:00
State: open

I'm interested in using the parse function introduced in 0.5 to pre-compile templates, but the code it generates relies on the `lookup`, `escapeHTML`, `renderSection`, and `render` functions which are not exposed via Mustache.

In order to provide those values to the code generated by parse, the `_compile` function returns a closure which calls the function, passing in all of the necessary private values, as well as catching the parse exception, and decorating it.

One solution would be to take this code

// This anonymous function wraps the generated function so we can do
// argument coercion, setup some variables, and handle any errors
// encountered while executing it.
return function (view, partials) {
partials = partials || {};

var stack = [view]; // context stack

try {
return fn(view, partials, stack, lookup, escapeHTML, renderSection, render);
} catch (e) {
throw debug(e.error, template, e.line, options.file);
}
};

from the `_compile` function, move it into a new function called `wrap` and export the `wrap` function.

Pre-compiled templates could then be bundled into functions, which would be passed to the `wrap` function to provide a function with access to the necessary inner state.