SilentVoid13/Templater

Frontmatter disappears when using 'Templater: Create...' command

Closed this issue · 1 comments

Frontmatter disappears when using 'Templater: Create...' command

Plugin information:

  • OS: macOS 11.7.10 (Big Sur)
  • Templater version: 2.9.0
  • Obsidian version: 1.7.4 (installer 1.7.4)
  • Templater settings: Template folder in vault root, template commands added to hotkeys section

Describe the bug
When using tp.file.create_new() and executing the template via "Templater: Create..." command, the frontmatter content is not retained in the new file. The frontmatter briefly appears but then gets wiped out when the cursor moves to the title field before returning to the note body.

Template code that reproduces the issue:

<%*
const newFile = await tp.file.create_new("---\ncreated: " + moment().format("YYYY.MM.DD HH:mm") + "\nmodified: " + moment().format("YYYY.MM.DD HH:mm") + "\ntags: []\n---\n\n", "Untitled", false, "~Inbox 📥");
const leaf = app.workspace.getLeaf('tab');
await leaf.openFile(newFile);
tp.hooks.on_all_templates_executed(async () => {
  leaf.view.editor.focus();
});
-%>

Expected behavior
The frontmatter should be retained in the new file when using "Templater: Create..." command.

Steps to reproduce:

  1. Save the above template as "mynewfile.md" in templates folder
  2. Add template to hotkeys section in Templater settings
  3. Execute via Command Palette "Templater: Create..."
  4. Observe frontmatter briefly appear then disappear

Additional context

  • Issue persists with all plugins disabled except Templater
  • Attempted workarounds using delays and app.vault.modify() don't resolve the issue
  • The behavior suggests a potential race condition in the Create command's execution path
  • This bug impacts workflows that need to create templated notes from any Obsidian context (e.g., from graph view, file explorer, or when no editor is focused)

You are creating two notes when you run the Templater: Create new note from template command that has tp.file.create_new() in it. The file created from tp.file.create_new() does have the frontmatter in it.

  1. Templater creates and opens a new file and starts executing.
  2. tp.file.create_new() is called, creating a new file called "Untitled" in the background in the inbox folder. This file will have the frontmatter.
  3. You explicitly open the newly created file using leaf.openFile(newNote), this is when the frontmatter briefly shows on the screen, because the note that was created from the previous step is being shown.
  4. Templater finishes executing, and shows you the note that was created from the first step, which won't have any content because you haven't set it up with any content.

I suspect you do not intend to create two notes, rather just one. In that case, you can use the Templater: Create new note from template command and have a template with the following content.

---
created: <% moment().format("YYYY.MM.DD HH:mm") %>
modified: <% moment().format("YYYY.MM.DD HH:mm") %>
tags: []
---

Then only one file will be created instead of two.


Marking as closed since this does not appear to be a bug with Templater. Feel free to continue replying in this thread if you need more support related to this issue.