19bdace85a
Extracts the parser to `@handlebars/parser`, where it can be shared between different implementations. This means that e.g. Glimmer/Ember will be able to iterate on new features without forcing Handlebars to adopt them immediately, and vice versa. All implementors will be able to absorb changes as it makes sense for them.
27 lines
613 B
JavaScript
27 lines
613 B
JavaScript
import { Exception } from '@handlebars/parser';
|
|
import { isEmpty, isFunction } from '../utils';
|
|
|
|
export default function(instance) {
|
|
instance.registerHelper('with', function(context, options) {
|
|
if (arguments.length != 2) {
|
|
throw new Exception('#with requires exactly one argument');
|
|
}
|
|
if (isFunction(context)) {
|
|
context = context.call(this);
|
|
}
|
|
|
|
let fn = options.fn;
|
|
|
|
if (!isEmpty(context)) {
|
|
let data = options.data;
|
|
|
|
return fn(context, {
|
|
data: data,
|
|
blockParams: [context]
|
|
});
|
|
} else {
|
|
return options.inverse(this);
|
|
}
|
|
});
|
|
}
|