Extend admin content templates
This commit is contained in:
+13
-1
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user