Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tp.file.create_new resolves templates in the output file too many times #1463

Open
camtauxe opened this issue Sep 25, 2024 · 4 comments
Open
Labels
bug Something isn't working

Comments

@camtauxe
Copy link

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.

@camtauxe camtauxe added the bug Something isn't working label Sep 25, 2024
@Zachatoo
Copy link
Collaborator

Zachatoo commented Oct 1, 2024

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.

await templater.overwrite_file_commands(file);

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)
%>

@Zachatoo
Copy link
Collaborator

Zachatoo commented Oct 1, 2024

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.

@camtauxe
Copy link
Author

camtauxe commented Oct 1, 2024

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

@marcaddeo
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants