How to auto call 2nd template to fix result of 1st template?

Now that yaml comments and double quotes around yaml values are auto deleted when creating notes with templater, a workaround was required and found: add a blank line in the template before the yaml starts, then use a 2nd template to remove the blank line.

(Or maybe I can add something to the 1st template that says: ‘when done, go up to first empty line and remove it’?)

My issue is that I can manually call the 2nd template and it works, but I can’t seem to combine the templates into one. I tried using await, and I tried using include to call the 2nd template, but nothing works.

Can someone help?

1st template (default file creation template)
<%* const title = await tp.system.prompt("Title"); -%>
<%* const folders = this.app.vault.getAllLoadedFiles().filter(i => i.children).map(folder => folder.path); 
const folder = await tp.system.suggester(folders, folders);
-%>
<%*
let titledate = title // note file name
let titleAddDate = tp.file.creation_date("YYYY-MM-DD") + " " + title
let isDate = (/^\d+-\d+-\d+/).test(title) // returns boolean
var result = ""
// check for presence of date in title
if(isDate){   // note title begins with YYYY-MM-DD
result = titledate
} else {
result = titleAddDate
}
titledate = result
-%>
<%*
let filename = title;
filename = filename.replace(/[\\\:*?<>|""]/g, "");
await tp.file.move(`/${folder}/${filename}`);
-%>
<%* 
let yamlline = "---" 
%>
<% yamlline %>
datecreated: <% tp.file.creation_date("YYYY-MM-DD") %>
titledate:  "<% titledate%>"
title: "<% titledate.substr(11, title.length)%>"
aliases: 
keywords: 
 - <% tp.file.folder(true).split('/').join('\n - ').toLowerCase() %>
status: active # archive, inactive
# personal
notes: >
<% yamlline %>
# <% titledate.substr(11, title.length) %>
<% tp.file.cursor(1) %>
2nd template (go up to 1st line and remove it)

I wonder if the issue is related to “activeLeaf”… because I am in the resulting file of template 1 when I call template 2. But if this template is inside my creation template, and I haven’t yet clicked on the new file because it isn’t finished being created, then maybe there is no active leaf to be found?

 <%*
// Get the current file content
let content = tp.file.content;
// Split content by lines, remove the first line (index 0)
let lines = content.split('\n');
lines.shift();
// Join the remaining lines back together
let newContent = lines.join('\n');
// Update the file content
await app.workspace.activeLeaf.view.editor.setValue(newContent);
%>

Note: When I tested combining the two templates I did rename “content” here, because it is already being used in the 1st template.

This worked for me. I added this line to your first template (doesn’t really matter where, I stuck it at the end), replace lise 2 with the name of your second template

<%* await tp.file.include("[[lise 2]]") -%>

And replaced the second template with this

<%*
tp.hooks.on_all_templates_executed(async () => {
  await tp.app.vault.process(tp.config.target_file, (content) => {
    let lines = content.split('\n');
    lines.shift();
    let newContent = lines.join('\n');
    return newContent;
  });
});
-%>

The problem was that tp.file.content is cached at execution time, we need to wait until Templater is finished executing (using the on_all_templates_executed hook), then re-read and update the file contents using vault.process.

Thank you so much! It works like a charm. This was my default template, so you’ve no idea how thankful I am - I use this template about 20 times per day!