2020-12-09 09:22:46 +00:00
|
|
|
import Helper from '@ember/component/helper';
|
|
|
|
import { inject as service } from '@ember/service';
|
2020-10-05 13:10:00 +00:00
|
|
|
|
2021-10-15 10:07:51 +00:00
|
|
|
// regexp that matches {{item.Name}} or ${item.Name}
|
|
|
|
// what this regex does
|
|
|
|
// (?:\$|\{) - Match either $ or {
|
|
|
|
// \{ - Match {
|
|
|
|
// ([a-z.0-9_-]+) - Capturing group
|
|
|
|
// (?:(?<=\$\{[^{]+) - Use a positive lookbehind to assert that ${ was matched previously
|
|
|
|
// |\} ) - or match a }
|
|
|
|
// \} - Match }
|
|
|
|
const templateRe = /(?:\$|\{)\{([a-z.0-9_-]+)(?:(?<=\$\{[^{]+)|\})\}/gi;
|
2020-12-09 09:22:46 +00:00
|
|
|
let render;
|
|
|
|
export default class RenderTemplateHelper extends Helper {
|
|
|
|
@service('encoder') encoder;
|
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
if (typeof render !== 'function') {
|
|
|
|
render = this.encoder.createRegExpEncoder(templateRe, encodeURIComponent, false);
|
|
|
|
}
|
2020-10-05 13:10:00 +00:00
|
|
|
}
|
2020-12-09 09:22:46 +00:00
|
|
|
|
|
|
|
compute([template, vars]) {
|
|
|
|
return render(template, vars);
|
|
|
|
}
|
|
|
|
}
|