Templater
Templater copied to clipboard
Using a dynamic command in frontmatter doesn't work
Plugin informations (please complete the following information):
- OS: Windows 10
- Templater version: 1.8.1
- Obsidian version: 0.12.10
- Templater settings: [e.g. Template folder location: "Templates", Timeout: "5"
Describe the bug Dynamic command does not display properly
Expected behavior Modification date: <%+ tp.file.last_modified_date() %> should display dymamic date when execute preview mode, but it display just plain text.
Screenshots
Please throw us a bone here @SilentVoid13 -san. 🙏
Related issues: #284, #250, #242
@petekrabs, here is my current workaround.
@welpdx Thanks so much, sadly this seems cant be applied on YAML header. :(
Sorry, I just realized that it works in the document but not in the yaml. Obsidian doesn't see it as a valid yaml format.
One could probably include a dynamic script within the document to add content to yaml when the script is run. Here's a quick nasty demo:
```
<%+*
let file = this.app.workspace.getActiveFile();
let content = tp.file.content;
let isYamlEmpty = Object.keys(tp.frontmatter).length === 0 && !content.match(/^-{3}\s*\n*\r*-{3}/);
var title = "Title"
var name = this.app.workspace.getActiveFile().name
let newFileContent = content.split("\n");
if (isYamlEmpty) {
newFileContent.unshift("---");
newFileContent.unshift(`${title}: ${name}`);
newFileContent.unshift("---");
}
else {
newFileContent.splice(1, 0, `${title}: ${name}`);
}
newFileContent = newFileContent.join("\n");
await app.vault.modify(file, newFileContent)
%>
```
But this dynamic code should be heavily redited to fit individual needs. The above code snippet was heavily inspired from MetaEdit.js
Wow that's a lot of hack, thank you so much!
@petekrabs Do you think this is something that will be fixed in the near future?
I ran into the same issue that the dynamic variable does not work in Metadata until I was looking into the github issue reports.
I saw @SilentVoid13 working on Templater recently (He just release update 1.9.9). Maybe he will take a look at this soon.
would love to see this
Hei, First of all, thanks. The plugin is fantastic and a daily problem solver. I am just wondering if there is any update on running "a dynamic command run in YAML header" such as:
---
modification date: <%+ tp.file.last_modified_date() %>
---
Since the dynamic command was added last year #63, the header metadata seems like the best place to store such an information and avoid clutering the notes. The issue has been raised a few times: #286, #495 and below from @welpdx #302
Please throw us a bone here @SilentVoid13 -san. 🙏 Related issues: #284, #250, #242 @petekrabs, here is my current workaround.
16 months after this issue was created, and I'm running into the same problem. Will this ever get fixed? @SilentVoid13
I would say it's fairly unlikely. To my novice eyes, it's a fairly difficult problem to solve. That said, y'all can open a discussion describing your use case and someone can try recommending a different plugin or workaround.
16 months after this issue was created, and I'm running into the same problem. Will this ever get fixed? @SilentVoid13
It works for me when wrapping the templater function in double quotes:
---
modification date: "<%+ tp.file.last_modified_date() %>"
---
Using the native obsidian frontmatter API, I came up with this much simpler and way less hacky solution:
<%*
// 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 with a very small delay.
setTimeout(() => {
app.fileManager.processFrontMatter(this.app.workspace.getActiveFile(), (frontmatter) => {
frontmatter["date"] = [`[[${tp.file.creation_date("YYYY-MM-DD")}]]`]
})
}, 1)
%>
16 months after this issue was created, and I'm running into the same problem. Will this ever get fixed? @SilentVoid13
It works for me when wrapping the templater function in double quotes:
--- modification date: "<%+ tp.file.last_modified_date() %>" ---
I just tried this and it didn't work for me.
16 months after this issue was created, and I'm running into the same problem. Will this ever get fixed? @SilentVoid13
It works for me when wrapping the templater function in double quotes:
--- modification date: "<%+ tp.file.last_modified_date() %>" ---
I just tried this and it didn't work for me.
Same, this works though.
Hi folks, just wanted to note that Zach and I plan to deprecate dynamic commands because it is fairly difficult to maintain. See further discussion on #913. Apologies in advance, but I would recommend looking for a different plugin to fit the use case if possible.