Templater icon indicating copy to clipboard operation
Templater copied to clipboard

Using a dynamic command in frontmatter doesn't work

Open LKittinun opened this issue 3 years ago • 15 comments

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 image image image

LKittinun avatar Jul 25 '21 05:07 LKittinun

Please throw us a bone here @SilentVoid13 -san. 🙏

Related issues: #284, #250, #242

@petekrabs, here is my current workaround.

welpdx avatar Jul 25 '21 21:07 welpdx

@welpdx Thanks so much, sadly this seems cant be applied on YAML header. :(

LKittinun avatar Jul 27 '21 11:07 LKittinun

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. gif

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: gif

```
<%+*
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

welpdx avatar Jul 27 '21 18:07 welpdx

Wow that's a lot of hack, thank you so much!

LKittinun avatar Jul 28 '21 05:07 LKittinun

@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.

blaettlerio avatar Oct 07 '21 14:10 blaettlerio

I saw @SilentVoid13 working on Templater recently (He just release update 1.9.9). Maybe he will take a look at this soon.

welpdx avatar Oct 08 '21 14:10 welpdx

would love to see this

venatiodecorus avatar Nov 16 '21 02:11 venatiodecorus

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.

pmlefeuvre avatar Jun 10 '22 09:06 pmlefeuvre

16 months after this issue was created, and I'm running into the same problem. Will this ever get fixed? @SilentVoid13

Arch-Storm avatar Oct 21 '22 06:10 Arch-Storm

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.

AB1908 avatar Oct 21 '22 09:10 AB1908

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

dleeftink avatar Nov 01 '22 11:11 dleeftink

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

xieve avatar Oct 26 '23 16:10 xieve

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.

ZerkerEOD avatar Oct 26 '23 18:10 ZerkerEOD

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.

xieve avatar Oct 29 '23 14:10 xieve

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.

AB1908 avatar Oct 29 '23 14:10 AB1908