tp.file.create_new resolves templates in the output file too many times
camtauxe opened this issue · 4 comments
Plugin information
- OS: NixOS
- Templater version: 2.7.1
- Obsidian version: 1.6.7
Describe the bug
I have a template I use to create other templates (call it "template template") in my templates folder:
template made on: <% tp.date.now("YYYY-MM-DD") %>
this note made on: <% '<'+'% tp.date.now("YYYY-MM-DD") %'+'>' %>
If I create a new note, then insert this template with Templater's sidebar button, I get the expected template out:
template made on: 2024-09-24
this note made on: <% tp.date.now("YYYY-MM-DD") %>
Now, I have yet another template that, when run, will generate a new template from this "template template"
<%
(async () => {
const template_template = tp.file.find_tfile("template template");
await tp.file.create_new(template_template, "new template")
})()
%>
But this time, the template it created in "new template" is already resolved! Making it useless as a template.
template made on: 2024-09-24
this note made on: 2024-09-24
Expected behavior
The resulting file when using tp.file.create_new
with a template should be the same as the resulting file when using that same template with Templater's own insert button.
Instead, the former resolves templates in the new file too many times.
This bug is being caused by this line, which is ran on file creation. We'll need to find a way to not run this line of code when using tp.file.create_new()
to resolve this bug.
Templater/src/core/Templater.ts
Line 525 in da864da
I don't suspect I'll get around to solving this soon, but here's a workaround to get your template working.
<%*
const template_template = tp.file.find_tfile("template template");
const content = await app.vault.read(template_template);
await app.vault.create("new template.md", content)
%>
This is similar to #1381, except in their case they're running the Create new note from template
command instead of calling tp.file.create_new()
from the Open insert template modal
command. Fixing one of these will likely fix the other.
Thank you for the response!
For now, I have worked around it by simply going another layer deeper and writing the template within a template within another template. It looks messy, but it does work. I will try your workaround
I think I'm also having an issue related to this. I'm using the Local REST API plugin to append to my daily note but if the daily note doesn't exist, Templater seems to run twice and removes the content that was appended.