Skip to content

Instantly share code, notes, and snippets.

@lifeart
Created July 14, 2021 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lifeart/d24ad90c0687fd91d67523e527d27a7c to your computer and use it in GitHub Desktop.
Save lifeart/d24ad90c0687fd91d67523e527d27a7c to your computer and use it in GitHub Desktop.
glimmer inline components
import SecondComponent from 'foo/bar';
function MyComponent(args) { return { a: 1, b: 2, c: args.e } }
function hasHelperManagerFor() {
return true;
}
function hasComponentManagerFor() {
return true;
}
function hasModifierManagerFor() {
return true;
}
function wrapValue(value) {
if (typeof value === 'string' || typeof value === 'boolean' || typeof value === 'number') {
return function() {
return value;
}
} else if (typeof value === 'function') {
if (hasHelperManagerFor(value)) {
return value;
} else if (hasComponentManagerFor(value)) {
return value;
} else if (hasModifierManagerFor(value)) {
return value;
} else {
throw new Error('unknown value type');
}
}
}
function hbs({raw}, ...args) {
const template = [];
const scope = {};
raw.forEach((el, index) => {
template.push(el);
if (args.length) {
let value = args.shift();
scope[`reference${index}`] = wrapValue(value);
template.push(`{{reference${index}}}`);
}
});
const tpl = template.join('');
console.log(tpl);
return function (Component) {
return setComponentTemplate(Component, compile(tpl));
}
}
export default hbs`<div><${SecondComponent}/></div>`(MyComponent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment