Dynamic Commands of Templater doesn't work in frontmatter

I’m trying to use this code:

---
creation date: <% tp.file.creation_date() %> 
update: <%+ tp.file.last_modified_date("dddd Do MMMM YYYY HH:mm:ss") %>
---
# <% tp.file.title %> 

I tried also a file with:

---
creation date: <% tp.file.creation_date() %> 
update: <%+ tp.file.last_modified_date("dddd Do MMMM YYYY HH:mm:ss") %>
---
# <% tp.file.title %> 

now: <%+tp.date.now()%>
<%+ tp.file.last_modified_date("dddd Do MMMM YYYY HH:mm:ss") %>

And in all of these tests, the dynamic command of the templater did not work in the frontmatter but worked correctly in the body.
Am I doing something wrong?

Thank you for the support

1 Like

It’s invalid YAML.
“creation date” → “creation-date”

2 Likes

“Creation date” worked fine, it is the dynamic command that is not working as expected in the frontmatter:

Edit mode:

Reading mode:

1 Like

UPDATE:
Thanks to other tests and a question on Reddit I reached a “solution”.
The dynamic commands can’t be embedded in the YAML file because, as metadata, they can’t be dynamic.

The solution found is to encapsulate the modified date with apostrophes in order to render the date in preview mode.
this result is fine because my goal was to “only” see the creation and last modified date in the metadata block and for querying them I can always use the Dataview native “file.ctime” and “file.mtime”.

Edit mode
creation-date: 2022-05-20 09:17
modification-date: ‘<%+ tp.file.last_modified_date(“dddd Do MMMM YYYY HH:mm:ss”) %>’

Preview mode
creation-date: 2022-05-20 09:17
modification-date: ‘Wednesday 18th May 2022 10:27:49’

I really don’t have an answer on why if you use apostrophes it works as I wanted.
Probably the string format is rendered in preview mode and the date format no.
This is actually something to look into.

4 Likes

This solution did not work for me.
For me it only worked without the function argument.
ie, this worked for me:

<% tp.file.last_modified_date() %>

This didnt (which is the snipped given in the Templater reference.

<% tp.file.last_modified_date(format: string = "YYYY-MM-DD HH:mm") %>

As said, the proposed solution also didnt work.

Running:
Obsidian v0.14.15
Templater v1.12.0

I have a similar problem when using templater metadata stuff in data view;

<% tp.file.last_modified_date() %>

works, but with parameter it doesn’t. And

<%+ tp.file.folder() %>

doesn’t work at all in the dataview; it always shows the folder of the note the dataview is in. Didn’t find a solution unfortunately…

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.