Extend admin content templates

This commit is contained in:
dcconstructions
2026-06-26 11:10:18 +03:00
parent 931ac628b4
commit e4bfd93a67
13 changed files with 4268 additions and 45 deletions
+13 -1
View File
@@ -83,6 +83,18 @@ function renderTemplateTokens(html, block) {
});
}
function renderEachBlocks(html, block) {
return html.replace(/\{\{#each\s+([a-zA-Z0-9_.-]+)\s*\}\}([\s\S]*?)\{\{\/each\}\}/g, (_match, path, innerHtml) => {
const items = getByPath(block, path);
if (!Array.isArray(items)) {
throw new Error(`Each token "${path}" in block "${block.id}" must point to an array`);
}
return items.map((item) => renderTemplateTokens(innerHtml, item ?? {})).join("");
});
}
function scopeDuplicateIds(html, scope) {
const ids = [...html.matchAll(/\sid="([^"]+)"/g)].map((match) => match[1]);
if (!ids.length) {
@@ -112,7 +124,7 @@ async function renderBlock(block, renderCounts) {
const renderCount = renderCounts.get(templateName) ?? 0;
renderCounts.set(templateName, renderCount + 1);
const html = renderTemplateTokens(await readTemplate(templateName), block);
const html = renderTemplateTokens(renderEachBlocks(await readTemplate(templateName), block), block);
if (renderCount === 0 && block.scopeIds !== true) {
return html;