How to update front matter via Templater templates
· One min read
[[TemplaterPlugin|Templater (Obsidian Plugin)]] doesn't have a straightforward way to update the front matter of a note that already has content in it. However, you can use the native Obsidian API within a template to achieve the same thing.
Side note: this code block keeps getting wiped out. I had thought it was due to transitioning back to submodules. Now, I'm pretty sure it's due to the Templater setting to evaluate templates on file creation, so I removed the Templater delimiters. Put this snippet into a < %* % > block.
// When creating a new file, we would create a race condition between Templater and the native Obsidian APIs we're using here.
// We can circumvent that by utilizing hooks.
tp.hooks.on_all_templates_executed(async () => {
const file = tp.file.find_tfile(tp.file.path(true));
await tp.app.fileManager.processFrontMatter(file, (frontmatter) => {
frontmatter["day"] = [`[[${tp.date.now("YYYY-MM-DD", 0, tp.file.title, "YYYY-MM-DD")}]]`]
let tags = frontmatter["tags"] ?? []
tags += "Meetings/Scrum";
frontmatter["tags"] = tags;
});
});
References
- See this comment in the Templater GitHub.
- tp.hooks - Templater