Templater
Templater copied to clipboard
"NaN" (from dynamic command) spreading through my vault
- Windows 11
- Obsidian v1.0.3
- Templater 1.16.0
I have a dynamic command in many of my notes:
#### updated: <%+ tp.file.last_modified_date() %>
...that used to stay as is in editing mode, but display the modified date in reading mode.
As of a recent Templater update (the most recent?) any note I touch ends up with a #### updated: NaN
in it.
Same goes with the Last modified date: <%+ tp.file.last_modified_date() %>
code found on the dynamic commands Templater page.
Hey @trainbuffer, I'm unable to reproduce on my end, could you share your complete template? Did you try to restart obsidian after the update? One more thing are you using live preview or source mode on your editor?
Here's a test template:
---
creation date: <% tp.file.creation_date("dddd MMMM Do YYYY HH:mm:ss") %>
alias: []
tags: []
state:
---
[[+ home|Home]]
## <% tp.file.title %>
In most of my notes:
updated: <%+ tp.file.last_modified_date() %>
Copied from [here](https://silentvoid13.github.io/Templater/commands/dynamic-command.html)
Last modified date: <%+ tp.file.last_modified_date() %>
Results in:
---
creation date: Wednesday November 9th 2022 18:44:26
alias: []
tags: []
state:
---
[[+ home|Home]]
## Untitled
In most of my notes:
updated: NaN
Copied from [here](https://silentvoid13.github.io/Templater/commands/dynamic-command.html)
Last modified date: NaN
- Restarted multiple times.
- Using live preview
- Just tried on macOS (12.6.1) with a new vault. The same. test-templater.md
Same here on Windows 10.
HEADER Template
created: <% tp.file.creation_date("dddd DD.MM.YYYY, HH:mm") %> cssclass: aliases:
<%+ tp.file.path(true) %>
<%+ tp.file.last_modified_date("dddd DD.MM.YYYY, HH:mm") %>
NOTE Template
<% tp.file.include("[[HEADER Template]]") %> <% tp.file.rename(tp.date.now("YYYY MM DD HHmm SS")) -%>
Result EDIT
2022 11 09 1143 52 created: Mittwoch 09.11.2022, 11:43 cssclass: aliases:
NaN
NaN
Pardon. Now in pretty :)
HEADER Template
---
created: <% tp.file.creation_date("dddd DD.MM.YYYY, HH:mm") %>
cssclass:
aliases:
---
###### <%+ tp.file.path(true) %>
###### <%+ tp.file.last_modified_date("dddd DD.MM.YYYY, HH:mm") %>
NOTE Template
<% tp.file.include("[[HEADER Template]]") %>
<% tp.file.rename(tp.date.now("YYYY MM DD HHmm SS")) -%>
---
Result in Edit Mode
---
created: Mittwoch 09.11.2022, 11:59
cssclass:
aliases:
---
###### NaN
###### NaN
---
Result in Preview Mode
NaN NaN
Could not repro on 1.15.2 but able to repro on 1.16.0. It would appear that the tp.file.include
resolves the dynamic <%+
commands when it should drop them in as is IMO. See output on 1.15.2:
---
created: Wednesday 09.11.2022, 07:08
cssclass:
aliases:
---
###### <%+ tp.file.path(true) %>
###### <%+ tp.file.last_modified_date("dddd DD.MM.YYYY, HH:mm") %>
---
vs 1.16.0:
---
created: Wednesday 09.11.2022, 07:08
cssclass:
aliases:
---
###### NaN
###### NaN
---
@SilentVoid13 while you're here, may I suggest deprecating dynamic commands? These are better covered by a few other plugins and they seem to need a fair number of fixes and support from our end. Happy to chat further if you have any concerns/thoughts.
Hey @AB1908 I've been willing to deprecate dynamic commands for a long time, or at least refactor them. But it looks like a fair number of people are still using them, so I'm not sure I want to break their workflows.
About the bug, I've managed to reproduce it, I'll try to find a quick fix today or tomorrow You can downgrade to 1.15.x if you don't want to wait
@SilentVoid13 @AB1908
Thank you 🙏🏼 Have daily vault backups but went into a momentary panic when I thought my entire vault had become NaN-ified. Thankfully it was only a few notes before I noticed what was happening.
I can wait for the update. Cheers for Templater and quickly sorting this!
@AB1908 Out of curiosity, what other plugins were you thinking of?
Dataview does this quite well IMO. Of course, I'm a bit biased here. Can you include a new Modal asking people to start migrating and also provide an estimated version number/date of deprecation? I'm willing to help be the bridge here.
Anyhow let's keep this on topic and start a separate discussion/issue to track.
Thanks a lot for your prompt response. I was not aware, that these dynamic commands are to be deprecated.
For the records. I replaced the dynamic Templater commands by Dataview queries, as follows.
HEADER Template
---
created: <% tp.file.creation_date("dddd DD.MM.YYYY, HH:mm") %>
cssclass:
aliases:
---
###### `=this.file.path`
###### `=dateformat(this.file.mtime, "DDDD, HH:mm")`
NOTE Template (as before)
<% tp.file.include("[[HEADER Template]]") %>
<% tp.file.rename(tp.date.now("YYYY MM DD HHmm SS")) -%>
---
I'm fine with deprecating the dynamic commands; I mean, it's your plugin - you do you. You've been doing great work so far.
But it would be nice to announce it more broadly, and give a timeline, and perhaps some tips on how to replace it in a couple of different ways.
^ I second this. A bit off topic of the NaN bug, but I posted this earlier on Discord and think it relevant:
Looking through my templater templates (and pile of half-finished/draft templates), I can only find two dynamic commands. But the last_modified_date one is in a lot of my notes. Totally willing to shift to Dataview or something else.
TBH, this NaN business got me thinking about how much utility I actually get out of using last_modified_date with templater in reading view. Not much, really, since I mostly live in Live Preview now. Just something in my templates from when I started. Don't forget to "cut the ends off the pot roast" or something. 🤔
@FilSalustri I think I'll open a separate issue so that we can have folks chime in. I don't like the format of discussions TBH.
I meet the same problem.
`
created: <% tp.date.now("YYYY-MM-DD HH:mm") %> aliases: [<% tp.file.title %>] tags: [dailynote]
Modified:: <%+ tp.file.last_modified_date() %>
← [[<% tp.date.yesterday("YYYY-MM-DD") %>]] | [[<% tp.date.tomorrow("YYYY-MM-DD")%>]] →
`
The results are: `
created: 2022-11-15 10:37 aliases: [2022-11-15] tags: [dailynote]
Modified:: NaN
← [[2022-11-14]] | [[2022-11-16]] → `
Versions:
- Obisdian: 1.0.3
- Templater: 1.16.0
Please revert back to the previous version while we wait for a fix.
Also, have the same issue: macOs, Obsidian 1.0.3, Templater 1.16.0
Template:
---
aliases:
creation date: <% tp.file.creation_date() %>
---
up::
tags::
modification date: <%+ tp.file.last_modified_date("dddd Do MMMM YYYY HH:mm:ss") %>
Last modified date: <%+ tp.file.last_modified_date() %>
Result
---
aliases:
creation date: 2022-11-22 19:30
---
up::
tags::
modification date: NaN
Last modified date: NaN
Hi! I‘m having the same issue with dynamic „last-motified-date“ in my Templates. Also getting an „NaN“ since about 14 days or so. Is there or will there be a fix for that? Besides that: Thanks for your Plugin!
In fact, the problem doesn't seem to be related to the dynamic command itself but to the fact that the dynamic command is passed through a template in a note. If you put directly into your note <%+ tp.file.last_modified_date() %> (in the frontmatter or in the note itself), the dynamic command works (because of the cache you have to go to another note and come back to see the change). If you put the dynamic command in the template, it doesn't work and a NaN appears. Is there any way to pass in the note the dynamic expression as "text"?
Nevertheless, thanks to @SilentVoid13 for this great plugin anyway!!!
I am using Templater v1.6.0 (in Obsidian v1.1.9, macOS Ventura 13.1).
I also suffered from this problem right away, but I didn't have time to spare, so I solved it temporarily. In the long run, this solution may become obsolete as it stops functioning.
My workaround is to include script files for dynamic commands from the outside.
For example, I defined the file with the following script on the outside to include updated_at
.
<%* return "<" + "%+ tp.file.last_modified_date('YYYY-MM-DD HH:mm:ss') %" + ">" %>
The key here is that we split the string to prevent the dynamic command from acting as a template inside.
Then I added a command to include the file into the template like this.
---
tags : 데일리노트
created_at: <% tp.file.creation_date('YYYY-MM-DD HH:mm:ss') %>
updated_at: <% tp.file.include('[[UpdatedAt Templater.snippet]]') %>
---
@SilentVoid13 Is there any news?
Hello??
Yo @AB1908
Is this problem because when you insert the templater or create a new file with the templater, the dynamic code is executed and returns NaN instead of Templater coping it over as a string?
I think Templater should detect all dynamic commands that start <%+
and instead of executing the code in the block, it should just ignore it and deal with it as text. This way, in the final note, the dynamic code is still there as <%+ tp.file.name #>
for example, and only when you preview it will the code inside be executed.
I think this is the issue right? We don't want the code to be executed on note creation or template insert, but only executed with the file is being viewed.
Yo @AB1908
Is this problem because when you insert the templater or create a new file with the templater, the dynamic code is executed and returns NaN instead of Templater coping it over as a string?
I think Templater should detect all dynamic commands that start
<%+
and instead of executing the code in the block, it should just ignore it and deal with it as text. This way, in the final note, the dynamic code is still there as<%+ tp.file.name #>
for example, and only when you preview it will the code inside be executed.I think this is the issue right? We don't want the code to be executed on note creation or template insert, but only executed with the file is being viewed.
Yes, I believe you're on point. Your description matches my weak interim "workaround":
- In the template, malform the leading dynamic template tag such that it isn't interpreted as a command upon render.
---
tags:
- oneliner
- template
oneliner: tbd
---
```bash
< tp.frontmatter.oneliner %>
```
^ that's a bash command, yo.
- Render the template and update the front matter, followed by completing the tag. Now it'll render as expected.
---
tags:
- oneliner
oneliner: |
echo "this is a super sweet oneliner"
---
```bash
<%+ tp.frontmatter.oneliner %>
```
^ that's a bash command, yo.
I prefer to use front matter for basic things like this because the content becomes available to Dataview and the dynamic command makes things DRY since it's "change once change everywhere".
It does make me a bit sad to hear dynamic commands may be going away, but I can understand why. Seems pretty challenging to implement and maintain.
Yes, I believe you're on point. Your description matches my weak interim "workaround":
Yes great to hear that
It does make me a bit sad to hear dynamic commands may be going away, but I can understand why. Seems pretty challenging to implement and maintain.
I think some people are looking into deprecating it so that people don't run into this same issue or misinterpret the usage of dynamic templates (because for most cases dataview is better). But don't worry, I don't think it is going away anytime soon. As SilentVoid points out, there is still alot of people using dynamic templates and so removing it will disrupt their workflow.
I think the best thing is for one of the maintainers or for SilentVoid to work on it to make dynamic templates better/work. But right now no one has that time/resources to do so. I wish I have time to learn typescript and do it but Im sticking to discussions and javascript for now.
I have one very huge use-case for dynamic templates: Obsidian doesn't allow markdown in properties, but it does allow dynamic templates it seems. This is the only way I've seen to add my modified date as a property. Hopefully this never goes away.
Also, this is still an issue.
The Linter plugin is a fantastic substitute for getting modified dates in frontmatter. Never had any issues with it.