Applying templates to an existing note - with template data before and after existing content

Hi All,
I was having a problem where I wanted to add dynamic headers and footers to an existing note.
I searched in most places, with some content aiding the way.
This ended up being my solution:

<%*
// header content
const head = `---
creation date: ${tp.file.creation_date()}
modification date: ${tp.file.last_modified_date("dddd Do MMMM YYYY HH:mm:ss")}
tags:
aliases:
---

`;
// footer content
const foot = `
---

File creation date: ${tp.file.creation_date()} | [[${tp.file.creation_date("YYYYMMDD")}]]
File modified date: ${tp.file.last_modified_date()} | [[${tp.file.last_modified_date("YYYYMMDD")}]]
`;

// Get the file object
const tf = tp.file.find_tfile(tp.file.title);

// Read the current file content
const content = await app.vault.read(tf);

// Write the new content
await app.vault.modify(tf, head + content + foot);
%>

Cheers