Skip to content

Commit

Permalink
Template boiler plate code
Browse files Browse the repository at this point in the history
  • Loading branch information
teshukatepalli1 committed Oct 17, 2023
1 parent 5cce32c commit 0ededf2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
29 changes: 29 additions & 0 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
waitForLCP,
loadBlocks,
loadCSS,
toClassName,
getMetadata,
} from './aem.js';

const LCP_BLOCKS = []; // add your LCP blocks to the list
Expand Down Expand Up @@ -42,6 +44,32 @@ async function loadFonts() {
}
}

const TEMPLATE_LIST = [
'red',
'green',
];

/**
* Run template specific decoration code.
* @param {Element} main The container element
*/
async function decorateTemplates(main) {
try {
const template = toClassName(getMetadata('template'));
const templates = TEMPLATE_LIST;
if (templates.includes(template)) {
const mod = await import(`../templates/${template}/${template}.js`);
loadCSS(`${window.hlx.codeBasePath}/templates/${template}/${template}.css`);
if (mod.default) {
await mod.default(main);
}
}
} catch (error) {
// eslint-disable-next-line no-console
console.error('Auto Blocking failed', error);
}
}

/**
* Builds all synthetic blocks in a container element.
* @param {Element} main The container element
Expand Down Expand Up @@ -79,6 +107,7 @@ async function loadEager(doc) {
const main = doc.querySelector('main');
if (main) {
decorateMain(main);
await decorateTemplates(main);
document.body.classList.add('appear');
await waitForLCP(LCP_BLOCKS);
}
Expand Down
1 change: 1 addition & 0 deletions templates/green/green.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* green template css */
1 change: 1 addition & 0 deletions templates/green/green.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// green template
1 change: 1 addition & 0 deletions templates/red/red.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* red template css */
1 change: 1 addition & 0 deletions templates/red/red.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// red template

0 comments on commit 0ededf2

Please sign in to comment.