summaryrefslogtreecommitdiffstats
path: root/lib/handlebars/helpers/with.js
blob: 7418cd0662f9c5d8b5ca9633eca77635fd340d19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import {appendContextPath, blockParams, createFrame, isEmpty, isFunction} from '../utils';

export default function(instance) {
  instance.registerHelper('with', function(context, options) {
    if (isFunction(context)) { context = context.call(this); }

    let fn = options.fn;

    if (!isEmpty(context)) {
      let data = options.data;
      if (options.data && options.ids) {
        data = createFrame(options.data);
        data.contextPath = appendContextPath(options.data.contextPath, options.ids[0]);
      }

      return fn(context, {
        data: data,
        blockParams: blockParams([context], [data && data.contextPath])
      });
    } else {
      return options.inverse(this);
    }
  });
}