Hi Nastja,
Yes, I have already reviewed this page. For external templates, you can use the template element and use an id to retrieve. However, the template function approach ultimately returns a string. This means that we cannot hook up event handlers, etc. If we want to encapsulate a Webix widget into a React component for example, we may want to be able to have a function that builds JSX and then renders it. There are a lot of other ways to build document fragments and we may also want to use something like lit-html and template stamping for example.
From the documentation:
Templates can be defined in five very flexible ways:
- as HTML strings within JavaScript objects
- by creating named templates
- by extracting them from an HTML container
- by retrieving them from an external file
- as a function returning a string
I am asking about support for a function that returns HTMLElement or an HTMLElement array. These would then be added to the host element using appendChild.
As an alternative, the function call could also pass in the host element and we could append the children directly. I guess the function could then return undefined or null to indicate that he have handled the rendering inside the function.
I did a little proof of concept on building a shim function what would accomplish what we want. If could be cleaner and ES5 friendly, but I just did the POC in Chrome and used string interpolation, fat arrow functions, etc. It uses requestAnimationFrame()
and querySelector()
, but this could be a lot more efficient if the host element was passed into the function. You can see it here: Snippet
The main helper function is this:
function templateFuncFactory(container, templateFunc) {
return function(...args) {
const content = templateFunc.call(this, ...args);
if (content instanceof HTMLElement) {
const phid = `ph${webix.uid()}`;
requestAnimationFrame(() => {
const placeholder = container.querySelector(`#${phid}`);
placeholder.parentElement.replaceChild(content, placeholder);
});
return `<div id="${phid}"></div>`;
}
return content;
};
}
Thanks,
Neal