blob: ee5f2f86e4b722d4a1965e4e5fa458e8e0354601 [file] [log] [blame]
part of mustache;
/// Passed as an argument to a mustache lambda function.
class _LambdaContext implements LambdaContext {
final _Node _node;
final _Renderer _renderer;
final bool _isSection;
bool _closed = false;
_LambdaContext(this._node, this._renderer, {bool isSection: true})
: _isSection = isSection;
void close() {
_closed = true;
_checkClosed() {
if (_closed) throw new _TemplateException(
'LambdaContext accessed outside of callback.',
_renderer._templateName, _renderer._source, _node.start);
/// Render the current section tag in the current context and return the
/// result as a string.
String renderString() {
return _renderer._renderSubtree(_node);
//FIXME Currently only return values are supported.
/// Render and directly output the current section tag.
// void render() {
// _checkClosed();
// }
//FIXME Currently only return values are supported.
/// Output a string.
// void write(Object object) {
// _checkClosed();
// }
/// Get the unevaluated template source for the current section tag.
String get source {
if (_node is! _SectionNode) return '';
var nodes = (_node as _SectionNode).children;
if (nodes.isEmpty) return '';
if (nodes.length == 1 && nodes.first is _TextNode)
return nodes.first.text;
var source = _renderer._source.substring(
_node.contentStart, _node.contentEnd);
return source;
/// Evaluate the string as a mustache template using the current context.
String renderSource(String source) {
var sink = new StringBuffer();
// Lambdas used for sections should parse with the current delimiters.
var delimiters = _isSection ? _renderer._delimiters : '{{ }}';
var node = _parse(source,
var renderer = new _Renderer.lambda(
return sink.toString();
/// Lookup the value of a variable in the current context.
Object lookup(String variableName) {
return _renderer._resolveValue(variableName);